-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebase.go
66 lines (62 loc) · 1.8 KB
/
firebase.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package firebase
import (
"context"
firebase "firebase.google.com/go"
"firebase.google.com/go/auth"
"google.golang.org/api/option"
"log"
"os"
"time"
)
const timeOutDelay = time.Duration(10)
/*SetupFirebaseFromEnv create a new *auth.Client, using env variable You must init
FIREBASE_KEY_PATH environment variable to the "root directory path"
to the credentials file
*/
func SetupFirebaseFromEnv() *auth.Client {
log.Printf("Setting up Firebase client.. \n")
if keyPath := os.Getenv("FIREBASE_KEY_PATH"); keyPath != "" {
opt := option.WithCredentialsFile(keyPath)
//Firebase admin SDK initialization
ctx, _ := context.WithTimeout(context.Background(), time.Second*timeOutDelay)
app, err := firebase.NewApp(ctx, nil, opt)
if err != nil {
panic(err)
}
//Firebase Auth
authCtx, _ := context.WithTimeout(context.Background(), time.Second*timeOutDelay)
auth, err := app.Auth(authCtx)
if err != nil {
panic(err)
}
log.Printf("Firebase client setup :) \n")
return auth
}
log.Printf("Failed to setup wireframe. \n")
return nil
}
/*SetupFirebase create a new *auth.Client. keyPath is the path to the
wireframe json file.
*/
func SetupFirebase(keyPath string) *auth.Client {
log.Printf("Setting up Firebase client.. \n")
if keyPath := os.Getenv(keyPath); keyPath != "" {
opt := option.WithCredentialsFile(keyPath)
//Firebase admin SDK initialization
ctx, _ := context.WithTimeout(context.Background(), time.Second*timeOutDelay)
app, err := firebase.NewApp(ctx, nil, opt)
if err != nil {
panic(err)
}
//Firebase Auth
authCtx, _ := context.WithTimeout(context.Background(), time.Second*timeOutDelay)
auth, err := app.Auth(authCtx)
if err != nil {
panic(err)
}
log.Printf("Firebase client setup :) \n")
return auth
}
log.Printf("Failed to setup wireframe. \n")
return nil
}