-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebase.go
77 lines (73 loc) · 1.93 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
67
68
69
70
71
72
73
74
75
76
77
package firebase
import (
"context"
firebase "firebase.google.com/go"
"firebase.google.com/go/auth"
"google.golang.org/api/option"
"log"
"os"
"time"
)
/*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, ctxErr := context.WithTimeout(context.Background(), time.Second * 10)
if ctxErr != nil {
panic(ctxErr)
}
app, err := firebase.NewApp(ctx, nil, opt)
if err != nil {
panic(err)
}
//Firebase Auth
authCtx, authCtxErr := context.WithTimeout(context.Background(), time.Second * 10)
if authCtxErr != nil {
panic(authCtxErr)
}
auth, err := app.Auth(authCtx)
if err != nil {
panic(err)
}
log.Printf("Firebase client setup :) \n")
return auth
}
log.Printf("Failed to setup firebase. \n")
return nil
}
/*SetupFirebase create a new *auth.Client. keyPath is the path to the
firebase 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, ctxErr := context.WithTimeout(context.Background(), time.Second * 10)
if ctxErr != nil {
panic(ctxErr)
}
app, err := firebase.NewApp(ctx, nil, opt)
if err != nil {
panic(err)
}
//Firebase Auth
authCtx, authCtxErr := context.WithTimeout(context.Background(), time.Second * 10)
if authCtxErr != nil {
panic(authCtxErr)
}
auth, err := app.Auth(authCtx)
if err != nil {
panic(err)
}
log.Printf("Firebase client setup :) \n")
return auth
}
log.Printf("Failed to setup firebase. \n")
return nil
}