-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
67 lines (58 loc) · 1.39 KB
/
service.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
package firebase
import (
"context"
"log"
"cloud.google.com/go/firestore"
firebase "firebase.google.com/go/v4"
auth "firebase.google.com/go/v4/auth"
"google.golang.org/api/option"
)
// Config represents the configuration
type Config struct {
FirebaseCredentials string
}
// Service represents the firebase service
type Service struct {
cfg *Config
cr Crypter
ctx context.Context
app *firebase.App
ac *auth.Client
fsc *firestore.Client
}
// New initializes firebase service with default config
func New(cfg *Config, cr Crypter) *Service {
// * Initialize Firebase Admin SDK with the fetched credentials
opt := option.WithCredentialsJSON([]byte(cfg.FirebaseCredentials))
ctx := context.Background()
app, err := firebase.NewApp(ctx, nil, opt)
if err != nil {
log.Fatalf("error initializing app: %v\n", err)
}
// * Initialize Auth client
ac, err := app.Auth(ctx)
if err != nil {
log.Fatalf("error initializing Auth client: %v\n", err)
}
// * Initialize Firestore client
fsc, err := app.Firestore(ctx)
if err != nil {
log.Fatalf("error initializing Firestore client: %v\n", err)
}
// ! gonna monitor if have more issues
// defer fsc.Close()
return &Service{
cfg: cfg,
cr: cr,
ctx: ctx,
app: app,
ac: ac,
fsc: fsc,
}
}
// Crypter represents security and stuff interface
type Crypter interface {
UID() string
GetCurrentUnixTime() int64
ScheduleToReset() int64
}