generated from antoniopaya22/go-rest-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.go
38 lines (33 loc) · 925 Bytes
/
admin.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
package fbauth
import (
"context"
"firebase.google.com/go/auth"
"github.com/pkg/errors"
)
func ElevateToAdmin(ctx context.Context, client *auth.Client, uid string) error {
claims := map[string]interface{}{"admin": true}
err := client.SetCustomUserClaims(ctx, uid, claims)
if err != nil {
return errors.Wrap(err, "error setting custom claims")
}
return nil
}
func RevokeAdmin(ctx context.Context, client *auth.Client, uid string) error {
err := client.SetCustomUserClaims(ctx, uid, nil)
if err != nil {
return errors.Wrap(err, "error revoking custom claims")
}
return nil
}
func VerifyAdmin(ctx context.Context, client *auth.Client, uid string) error {
user, err := client.GetUser(ctx, uid)
if err != nil {
return errors.Wrap(err, "failed to get user during verify admin")
}
if admin, ok := user.CustomClaims["admin"]; ok {
if admin.(bool) {
return nil
}
}
return errors.New("not admin")
}