/
create.go
98 lines (82 loc) · 2.38 KB
/
create.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package auth
import (
"context"
"strings"
"time"
"cloud.google.com/go/firestore"
"google.golang.org/grpc/codes"
"github.com/wheatandcat/memoir-backend/graph/model"
"github.com/wheatandcat/memoir-backend/repository"
ce "github.com/wheatandcat/memoir-backend/usecase/custom_error"
)
type Auth struct {
UserID string
CreatedAt time.Time
}
// CreateAuthUser 認証ユーザー作成
func (uci *useCaseImpl) CreateAuthUser(ctx context.Context, f *firestore.Client, input *model.NewAuthUser, u *repository.User, mu *model.AuthUser) (string, error) {
displayName := ""
app, err := repository.FirebaseApp(ctx)
if err != nil {
return displayName, ce.CustomError(err)
}
client, err := app.Auth(ctx)
if err != nil {
return displayName, ce.CustomError(err)
}
user, err := client.GetUser(ctx, u.FirebaseUID)
if err != nil {
return displayName, ce.CustomError(err)
}
arr1 := strings.Split(user.UserInfo.Email, "@")
if len(arr1) > 0 {
displayName = arr1[0]
}
u.DisplayName = displayName
aref := f.Collection("auth").Doc(u.FirebaseUID)
uref := f.Collection("users").Doc(u.ID)
err = f.RunTransaction(ctx, func(ctx context.Context, tx *firestore.Transaction) error {
_, err := tx.Get(aref)
if repository.GrpcErrorStatusCode(err) == codes.InvalidArgument || repository.GrpcErrorStatusCode(err) == codes.NotFound {
// 既にユーザー作成済みの場合は更新しないで完了
a := Auth{
UserID: mu.ID,
CreatedAt: mu.CreatedAt,
}
err := tx.Set(aref, a)
if err != nil {
return ce.CustomError(err)
}
if input.IsNewUser {
u.CreatedAt = mu.CreatedAt
if err := tx.Set(uref, u); err != nil {
return ce.CustomError(err)
}
} else {
ds, err := uref.Get(ctx)
if err != nil {
return ce.CustomError(err)
}
dn, err := ds.DataAt("DisplayName")
if err != nil {
return ce.CustomError(err)
}
u.UpdatedAt = mu.UpdatedAt
var uu []firestore.Update
uu = append(uu, firestore.Update{Path: "FirebaseUID", Value: u.FirebaseUID})
if dn.(string) == "" {
// 名前が空ならメールアドレスで設定
uu = append(uu, firestore.Update{Path: "DisplayName", Value: u.DisplayName})
} else {
displayName = dn.(string)
}
if err = tx.Update(uref, uu); err != nil {
return ce.CustomError(err)
}
}
return nil
}
return nil
})
return displayName, ce.CustomError(err)
}