-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebase.go
124 lines (106 loc) · 3.29 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package azdrivers
import (
"context"
"log"
firebase "firebase.google.com/go"
"firebase.google.com/go/auth"
"google.golang.org/api/option"
)
type (
AZFirebaseApp struct {
app *firebase.App
auth *auth.Client
}
FirebaseUpdateOpts struct {
Email string `json:"email"`
EmailVerified bool `json:"email_verified"`
PhoneNumber string `json:"phone_number"`
Password string `json:"password"`
DisplayName string `json:"display_name"`
PhotoURL string `json:"photo_url"`
Disabled bool `json:"disabled"`
}
)
const (
// ProjectID = ""
)
var ()
func NewFirebaseApp(credentailsPath string) (*AZFirebaseApp, error) {
azfp := new(AZFirebaseApp)
err := azfp.init(credentailsPath)
if err != nil {
return nil, err
}
log.Println("firebase driver init")
return azfp, nil
}
func (azfb *AZFirebaseApp) init(credentailsPath string) error {
opt := option.WithCredentialsFile(credentailsPath)
ctx := context.Background()
var err error
azfb.app, err = firebase.NewApp(ctx, nil, opt)
if err != nil {
// log.Fatal(err)
log.Println("azdrivers - firebase -init error:", err)
return err
}
return azfb.authorize()
}
func (azfb *AZFirebaseApp) authorize() error {
var err error
ctx := context.Background()
azfb.auth, err = azfb.app.Auth(ctx)
if err != nil {
log.Println("azdrivers - firebase -authorize error:", err)
return err
}
return nil
}
func (azfb *AZFirebaseApp) GetUser(email string) (*auth.UserRecord, error) {
ctx := context.Background()
return azfb.auth.GetUserByEmail(ctx, email)
}
func (azfb *AZFirebaseApp) InitCustomToken(uid string, customData map[string]interface{}) (string, error) {
ctx := context.Background()
return azfb.auth.CustomTokenWithClaims(ctx, uid, customData)
}
func (azfb *AZFirebaseApp) InitNewUser(email, pwd, displayName string) (*auth.UserRecord, error) {
ctx := context.Background()
userParams := (&auth.UserToCreate{}).Email(email).Password(pwd).DisplayName(displayName).Disabled(false)
return azfb.auth.CreateUser(ctx, userParams)
}
func (azfb *AZFirebaseApp) UpdateUser(uid string, updateData FirebaseUpdateOpts) (*auth.UserRecord, error) {
ctx := context.Background()
userUpdateRecord := &auth.UserToUpdate{}
if updateData.Email != "" {
userUpdateRecord = userUpdateRecord.Email(updateData.Email)
}
if updateData.Password != "" {
userUpdateRecord = userUpdateRecord.Password(updateData.Password)
}
if updateData.DisplayName != "" {
userUpdateRecord = userUpdateRecord.DisplayName(updateData.DisplayName)
}
if updateData.PhoneNumber != "" {
userUpdateRecord = userUpdateRecord.PhoneNumber(updateData.PhoneNumber)
}
if updateData.PhotoURL != "" {
userUpdateRecord = userUpdateRecord.PhotoURL(updateData.PhotoURL)
}
return azfb.auth.UpdateUser(ctx, uid, userUpdateRecord)
}
func (azfb *AZFirebaseApp) DeleteUser(uid string) error {
ctx := context.Background()
return azfb.auth.DeleteUser(ctx, uid)
}
func (azfb *AZFirebaseApp) VerifyClientToken(tokenString string, checkRevocation bool) (*auth.Token, error) {
ctx := context.Background()
if checkRevocation {
return azfb.auth.VerifyIDTokenAndCheckRevoked(ctx, tokenString)
}
return azfb.auth.VerifyIDToken(ctx, tokenString)
}
func (azfb *AZFirebaseApp) RevokeRefreshTokens(uid string) error {
ctx := context.Background()
return azfb.auth.RevokeRefreshTokens(ctx, uid)
}