-
Notifications
You must be signed in to change notification settings - Fork 7
/
firebase.go
103 lines (82 loc) · 2.4 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
package security
import (
"context"
"firebase.google.com/go/auth"
"github.com/colibri-project-io/colibri-sdk-go/pkg/base/cloud"
"github.com/colibri-project-io/colibri-sdk-go/pkg/base/logging"
)
type firebaseAuthService struct {
client *auth.Client
}
func newFirebaseAuthenticationService() *firebaseAuthService {
auth, err := cloud.GetFirebaseSession().Auth(context.Background())
if err != nil {
logging.Fatal(connection_error, err)
}
return &firebaseAuthService{auth}
}
func (s *firebaseAuthService) GetUser(ctx context.Context, id string) (*User, error) {
user, err := s.client.GetUser(ctx, id)
if err != nil {
return nil, err
}
return &User{
ID: user.UID,
Email: user.Email,
Name: user.DisplayName,
}, nil
}
func (s *firebaseAuthService) CreateUser(ctx context.Context, user *UserCreate) error {
userToCreate := (&auth.UserToCreate{}).
UID(user.ID).
Email(user.Email).
Password(user.Password).
DisplayName(user.Name)
if user.PhotoURL != "" {
userToCreate.PhotoURL(user.PhotoURL)
}
if _, err := s.client.CreateUser(ctx, userToCreate); err != nil {
return err
}
userToSetClaims := &auth.UserToUpdate{}
userToSetClaims.CustomClaims(map[string]any{
profileField: user.Profile,
tenantIdField: user.TenantID,
})
_, err := s.client.UpdateUser(ctx, user.ID, userToSetClaims)
return err
}
func (s *firebaseAuthService) UpdateUser(ctx context.Context, id string, user *UserUpdate) error {
userToUpdate := &auth.UserToUpdate{}
if user.Email != "" {
userToUpdate.Email(user.Email)
}
if user.Password != "" {
userToUpdate.Password(user.Password)
}
if user.Name != "" {
userToUpdate.DisplayName(user.Name)
}
if user.PhotoURL != "" {
userToUpdate.PhotoURL(user.PhotoURL)
}
userToUpdate.CustomClaims(map[string]any{
profileField: user.Profile,
tenantIdField: user.TenantID,
})
_, err := s.client.UpdateUser(ctx, id, userToUpdate)
return err
}
func (s *firebaseAuthService) DeleteUser(ctx context.Context, id string) error {
return s.client.DeleteUser(ctx, id)
}
func (s *firebaseAuthService) EnableUser(ctx context.Context, id string) error {
userToUpdate := (&auth.UserToUpdate{}).Disabled(false)
_, err := s.client.UpdateUser(ctx, id, userToUpdate)
return err
}
func (s *firebaseAuthService) DisableUser(ctx context.Context, id string) error {
userToUpdate := (&auth.UserToUpdate{}).Disabled(true)
_, err := s.client.UpdateUser(ctx, id, userToUpdate)
return err
}