-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
63 lines (52 loc) · 1.35 KB
/
client.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
package auth
import (
"context"
"fmt"
firebase "firebase.google.com/go"
"firebase.google.com/go/auth"
"github.com/samthehai/chat/internal/domain/entity"
"google.golang.org/api/option"
)
type FirebaseClient struct {
authClient *auth.Client
}
func NewFirebaseClient(
ctx context.Context,
firebaseCredentials string,
) (*FirebaseClient, error) {
app, err := firebase.NewApp(
ctx,
nil,
option.WithCredentialsJSON([]byte(firebaseCredentials)),
)
if err != nil {
return nil, fmt.Errorf("initialize firebase app: %w", err)
}
client, err := app.Auth(ctx)
if err != nil {
return nil, fmt.Errorf("initialize firebase auth client: %w", err)
}
return &FirebaseClient{authClient: client}, nil
}
func (m *FirebaseClient) VerifyIDToken(
ctx context.Context,
idToken string,
) (*entity.AuthToken, error) {
if idToken == "" {
err := fmt.Errorf("verify id token: invalid idToken")
return nil, err
}
token, err := m.authClient.VerifyIDToken(ctx, idToken)
if err != nil {
err := fmt.Errorf("verify id token: %w", err)
return nil, err
}
return &entity.AuthToken{
UserID: token.UID,
Name: token.Claims["name"].(string),
PictureUrl: token.Claims["picture"].(string),
Provider: token.Firebase.SignInProvider,
EmailAddress: token.Claims["email"].(string),
EmailVerified: token.Claims["email_verified"].(bool),
}, nil
}