-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebase.go
51 lines (41 loc) · 1.11 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
package tomeit
import (
"context"
"fmt"
"log"
"os"
firebase "firebase.google.com/go/v4"
"firebase.google.com/go/v4/auth"
"google.golang.org/api/option"
)
type firebaseAppInterface interface {
verifyIDToken(ctx context.Context, idToken string) (*auth.Token, error)
}
type FirebaseApp struct {
*firebase.App
}
func InitFirebaseApp() *FirebaseApp {
app, err := firebase.NewApp(context.Background(), nil, option.WithCredentialsJSON([]byte(os.Getenv("GOOGLE_CREDENTIALS_JSON"))))
if err != nil {
log.Fatalln("Init firebase app failed:", err)
}
return &FirebaseApp{app}
}
func (app *FirebaseApp) verifyIDToken(ctx context.Context, idToken string) (*auth.Token, error) {
client, err := app.Auth(ctx)
if err != nil {
return nil, fmt.Errorf("auth failed: %w", err)
}
token, err := client.VerifyIDToken(ctx, idToken)
if err != nil {
return nil, fmt.Errorf("verifyIDToken failed: %w", err)
}
return token, nil
}
type firebaseAppMock struct{}
func (app *firebaseAppMock) verifyIDToken(ctx context.Context, idToken string) (*auth.Token, error) {
token := auth.Token{
UID: "someUserUID",
}
return &token, nil
}