-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebase.go
39 lines (33 loc) · 1007 Bytes
/
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
package firebases
import (
"context"
"net/http"
"os"
firebase "firebase.google.com/go"
"github.com/ichi-pg/golang-server/internal/domain"
"github.com/ichi-pg/golang-server/internal/pkg/env"
"google.golang.org/api/option"
)
type firebaseService struct {
}
// FirebaseRepository はFirebaseRepositoryの実装を返します。
func FirebaseRepository() domain.FirebaseRepository {
return firebaseService{}
}
func (s firebaseService) FirebaseID(c context.Context, token domain.FirebaseToken) (domain.FirebaseID, error) {
opt := option.WithCredentialsJSON([]byte(os.Getenv(env.FirebaseJSON)))
app, err := firebase.NewApp(c, nil, opt)
if err != nil {
return domain.FirebaseID(""), err
}
auth, err := app.Auth(c)
if err != nil {
return domain.FirebaseID(""), err
}
res, err := auth.VerifyIDToken(c, string(token))
if err != nil {
return domain.FirebaseID(""), domain.NewRequestError(http.StatusUnauthorized, err.Error())
}
return domain.FirebaseID(res.UID), nil
}
//TODO test code