@@ -21,7 +21,7 @@ type IAuthService interface {
21
21
VerifyLocalAuthToken (token string , email string ) (string , error )
22
22
Callback (c * fiber.Ctx ) (string , error )
23
23
GetUser (auth_user goth.User ) (* User , * AuthIdentity , error )
24
- Register (auth_user goth. User ) (* User , error )
24
+ Register (options * RegistrationOptions ) (* User , error )
25
25
RegisterProviders ()
26
26
IsAuthenticated (token string ) (* JWTClaim , error )
27
27
}
@@ -89,7 +89,7 @@ func (a *AuthServiceImpl) VerifyLocalAuthToken(token string, email string) (stri
89
89
return jwt_token , nil
90
90
}
91
91
92
- user , err := a .Register (goth_user )
92
+ user , err := a .Register (withEmailPrefix ( goth_user ) )
93
93
94
94
if err != nil {
95
95
return "" , err
@@ -101,6 +101,7 @@ func (a *AuthServiceImpl) VerifyLocalAuthToken(token string, email string) (stri
101
101
}
102
102
103
103
func (a * AuthServiceImpl ) Callback (c * fiber.Ctx ) (string , error ) {
104
+ provider := c .Params ("provider" )
104
105
auth_user , err := goth_fiber .CompleteUserAuth (c )
105
106
if err != nil {
106
107
log .Error (err )
@@ -117,7 +118,12 @@ func (a *AuthServiceImpl) Callback(c *fiber.Ctx) (string, error) {
117
118
return token , nil
118
119
}
119
120
120
- user_md , err = a .Register (auth_user )
121
+ log .Info (auth_user .NickName )
122
+ if provider == "github" {
123
+ user_md , err = a .Register (withGithubUsername (auth_user ))
124
+ } else {
125
+ user_md , err = a .Register (withEmailPrefix (auth_user ))
126
+ }
121
127
122
128
if err != nil {
123
129
return "" , err
@@ -140,19 +146,41 @@ func (a *AuthServiceImpl) GetUser(auth_user goth.User) (*User, *AuthIdentity, er
140
146
return & auth_and_user .User , & auth_and_user .AuthIdentity , nil
141
147
}
142
148
143
- func (a * AuthServiceImpl ) Register (auth_user goth.User ) (* User , error ) {
149
+ type RegistrationOptions struct {
150
+ AuthUser goth.User
151
+ SqlUser * UserSQL
152
+ }
153
+
154
+ func withEmailPrefix (user goth.User ) * RegistrationOptions {
155
+ return & RegistrationOptions {
156
+ SqlUser : & UserSQL {
157
+ ID : sql.NullString {String : user .UserID , Valid : true },
158
+ Email : sql.NullString {String : user .Email , Valid : true },
159
+ Name : sql.NullString {String : strings .Split (user .Email , "@" )[0 ], Valid : true },
160
+ Picture : sql.NullString {String : user .AvatarURL , Valid : true },
161
+ },
162
+ AuthUser : user ,
163
+ }
164
+ }
165
+
166
+ func withGithubUsername (user goth.User ) * RegistrationOptions {
167
+ return & RegistrationOptions {SqlUser : & UserSQL {
168
+ ID : sql.NullString {String : user .UserID , Valid : true },
169
+ Email : sql.NullString {String : user .Email , Valid : true },
170
+ Name : sql.NullString {String : user .NickName , Valid : true },
171
+ Picture : sql.NullString {String : user .AvatarURL , Valid : true },
172
+ }, AuthUser : user }
173
+ }
174
+
175
+ func (a * AuthServiceImpl ) Register (options * RegistrationOptions ) (* User , error ) {
176
+ log .Info (options )
177
+ auth_user := options .AuthUser
144
178
data , err := json .Marshal (auth_user )
145
179
if err != nil {
146
180
return nil , errors .New ("couldn't marshal user" )
147
181
}
148
182
149
- user_model := UserSQL {
150
- ID : sql.NullString {String : auth_user .UserID , Valid : true },
151
- Email : sql.NullString {String : auth_user .Email , Valid : true },
152
- Name : sql.NullString {String : auth_user .Name , Valid : true },
153
- Picture : sql.NullString {String : auth_user .AvatarURL , Valid : true },
154
- }
155
-
183
+ user_model := options .SqlUser
156
184
user_data , err := user_model .Save ()
157
185
158
186
if err != nil {
0 commit comments