-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase.go
55 lines (43 loc) · 1.12 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
package firebase
import (
"context"
"net/http"
firebase "firebase.google.com/go/v4"
"github.com/morning-night-guild/platform-app/internal/adapter/external"
"github.com/morning-night-guild/platform-app/internal/domain/model/errors"
"github.com/morning-night-guild/platform-app/pkg/log"
"google.golang.org/api/option"
)
type Firebase struct{}
func New() *Firebase {
return &Firebase{}
}
func (fb *Firebase) Of(
secret string,
endpoint string,
apiKey string,
) (*external.Auth, error) {
opt := option.WithCredentialsJSON([]byte(secret))
app, err := firebase.NewApp(context.Background(), nil, opt)
if err != nil {
log.Log().Error("error initializing app", log.ErrorField(err))
return nil, err
}
auth, err := app.Auth(context.Background())
if err != nil {
log.Log().Error("error create auth client", log.ErrorField(err))
return nil, err
}
if endpoint == "" {
return nil, errors.NewValidationError("firebase endpoint is empty")
}
if apiKey == "" {
return nil, errors.NewValidationError("firebase api key is empty")
}
return external.NewAuth(
endpoint,
apiKey,
http.DefaultClient,
auth,
), nil
}