generated from antoniopaya22/go-rest-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fbauth.go
47 lines (37 loc) · 1.18 KB
/
fbauth.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
package fbauth
import (
"context"
"os"
firebase "firebase.google.com/go"
"firebase.google.com/go/auth"
"github.com/pkg/errors"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
secretsmanager "cloud.google.com/go/secretmanager/apiv1"
"github.com/atsur/api-server/pkg/storage"
)
var (
firebaseConfigFile = os.Getenv("FIREBASE_CONFIG_FILE")
)
func InitAuth() (*auth.Client, error) {
file, err := storage.ReadFile(firebaseConfigFile)
if err != nil {
return nil, errors.Wrap(err, "error reading firebase config file (init auth)")
}
ctx := context.Background()
creds, err := google.CredentialsFromJSON(ctx, file, secretsmanager.DefaultAuthScopes()...)
if err != nil {
return nil, errors.Wrap(err, "error getting cloud credentials from json (init auth))")
}
opt := option.WithCredentials(creds)
// opt := option.WithCredentialsFile(firebaseConfigFile)
app, err := firebase.NewApp(ctx, nil, opt)
if err != nil {
return nil, errors.Wrap(err, "error initializing firebase auth (create firebase app)")
}
client, errAuth := app.Auth(ctx)
if errAuth != nil {
return nil, errors.Wrap(errAuth, "error initializing firebase auth (creating client)")
}
return client, nil
}