/
sign_in_google.go
156 lines (135 loc) · 5.06 KB
/
sign_in_google.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package resolvers
import (
"context"
"strconv"
"time"
"github.com/doug-martin/goqu/v9"
"github.com/mattdamon108/go-graphql-api-boilerplate/model"
"github.com/mattdamon108/go-graphql-api-boilerplate/utils"
)
type Claims struct {
Sub string
Email string
}
// SignInGoogle mutation
func (r *Resolvers) SignInGoogle(ctx context.Context, args signInGoogleMutationArgs) (*SignInGoogleResponse, error) {
// check if exist 1) by googleID 2) by email
// 1) googleId exists -> sign in
// 2) email exists -> update googleId -> sign in
// 3) otherwise sign up
claims, err := utils.GoogleSignIn(ctx, args.IdToken)
userAndSocial := model.UserAndSocial{}
found, err := r.DB.
From("user").
Join(goqu.T("user_social"), goqu.On(goqu.I("user.id").Eq(goqu.I("user_social.user_id")))).
Where(goqu.I("user_social.google").Eq(claims.Sub)).
ScanStruct(&userAndSocial)
if err != nil {
// fatal error
msg := "Failed to sign in with google"
return &SignInGoogleResponse{Status: false, Msg: &msg, Token: nil}, nil
}
if !found {
// no matching googleId -> query by email
found, err := r.DB.
From("user").
Join(goqu.T("user_social"), goqu.On(goqu.I("user.id").Eq(goqu.I("user_social.user_id")))).
Where(goqu.I("user.email").Eq(claims.Email)).
ScanStruct(&userAndSocial)
if err != nil {
// fatal error
msg := "Failed to sign in with google"
return &SignInGoogleResponse{Status: false, Msg: &msg, Token: nil}, nil
}
if !found {
// no matching googleId, no matching email -> sign up
tx, err := r.DB.Begin()
if err != nil {
msg := "Failed to sign in with google: transaction failed to begin"
return &SignInGoogleResponse{Status: false, Msg: &msg, Token: nil}, nil
}
insert := tx.Insert("user").Rows(goqu.Record{"email": claims.Email, "nickname": claims.Name}).Executor()
result, err := insert.Exec()
if err != nil {
if rErr := tx.Rollback(); rErr != nil {
msg := "Failed to sign in with google: transaction failed to be rolled back"
return &SignInGoogleResponse{Status: false, Msg: &msg, Token: nil}, nil
}
msg := "Failed to sign in with google: transaction rolled back"
return &SignInGoogleResponse{Status: false, Msg: &msg, Token: nil}, nil
}
id, err := result.LastInsertId()
if err != nil {
if rErr := tx.Rollback(); rErr != nil {
msg := "Failed to sign in with google: transaction failed to be rolled back"
return &SignInGoogleResponse{Status: false, Msg: &msg, Token: nil}, nil
}
msg := "Failed to sign in with google: failed to insert user"
return &SignInGoogleResponse{Status: false, Msg: &msg, Token: nil}, nil
}
insert = tx.Insert("user_social").Rows(goqu.Record{"user_id": id, "google": claims.Sub}).Executor()
if _, err := insert.Exec(); err != nil {
if rErr := tx.Rollback(); rErr != nil {
msg := "Failed to sign in with google: transaction failed to be rolled back"
return &SignInGoogleResponse{Status: false, Msg: &msg, Token: nil}, nil
}
msg := "Failed to sign in with kakao: transaction rolled back"
return &SignInGoogleResponse{Status: false, Msg: &msg, Token: nil}, nil
}
if err = tx.Commit(); err != nil {
msg := "Failed to sign in with kakao: transaction failed to commit"
return &SignInGoogleResponse{Status: false, Msg: &msg, Token: nil}, nil
}
userIDString := strconv.Itoa(int(id))
tokenString, err := utils.SignJWT(&userIDString)
if err != nil {
msg := "Error in generating JWT"
return &SignInGoogleResponse{Status: false, Msg: &msg, Token: nil}, nil
}
return &SignInGoogleResponse{Status: false, Msg: nil, Token: tokenString}, nil
} else {
// no matching googleId, but email -> update googleId -> sign in
insert := r.DB.
Update("user_social").
Set(goqu.Record{"google": claims.Sub, "updated_at": time.Now()}).
Where(goqu.C("user_id").Eq(userAndSocial.User.ID)).
Executor()
if _, err := insert.Exec(); err != nil {
msg := "Failed to sign in with google: failed to save user social"
return &SignInGoogleResponse{Status: false, Msg: &msg, Token: nil}, nil
}
userIDString := strconv.Itoa(int(userAndSocial.User.ID))
tokenString, err := utils.SignJWT(&userIDString)
if err != nil {
msg := "Error in generating JWT"
return &SignInGoogleResponse{Status: false, Msg: &msg, Token: nil}, nil
}
return &SignInGoogleResponse{Status: true, Msg: nil, Token: tokenString}, nil
}
}
// matching googleId -> happy!
userIDString := strconv.Itoa(int(userAndSocial.User.ID))
tokenString, err := utils.SignJWT(&userIDString)
if err != nil {
msg := "Error in generating JWT"
return &SignInGoogleResponse{Status: false, Msg: &msg, Token: nil}, nil
}
return &SignInGoogleResponse{Status: true, Msg: nil, Token: tokenString}, nil
}
type signInGoogleMutationArgs struct {
IdToken string
}
// SignInResponse is the response type
type SignInGoogleResponse struct {
Status bool
Msg *string
Token *string
}
// Ok for SignUpResponse
func (r *SignInGoogleResponse) Ok() bool {
return r.Status
}
// Error for SignUpResponse
func (r *SignInGoogleResponse) Error() *string {
return r.Msg
}