@@ -171,7 +171,12 @@ async def create(
171
171
172
172
async def oauth_callback (
173
173
self : "BaseUserManager[models.UOAP]" ,
174
- oauth_account : models .OAP ,
174
+ oauth_name : str ,
175
+ access_token : str ,
176
+ account_id : str ,
177
+ account_email : str ,
178
+ expires_at : Optional [int ] = None ,
179
+ refresh_token : Optional [str ] = None ,
175
180
request : Optional [Request ] = None ,
176
181
) -> models .UOAP :
177
182
"""
@@ -185,44 +190,53 @@ async def oauth_callback(
185
190
If the user does not exist, it is created and the on_after_register handler
186
191
is triggered.
187
192
188
- :param oauth_account: The new OAuth account to create.
193
+ :param oauth_name: Name of the OAuth client.
194
+ :param access_token: Valid access token for the service provider.
195
+ :param account_id: ID of the user on the service provider.
196
+ :param account_email: E-mail of the user on the service provider.
197
+ :param expires_at: Optional timestamp at which the access token expires.
198
+ :param refresh_token: Optional refresh token to get a
199
+ fresh access token from the service provider.
189
200
:param request: Optional FastAPI request that
190
201
triggered the operation, defaults to None
191
202
:return: A user.
192
203
"""
204
+ oauth_account_dict = {
205
+ "oauth_name" : oauth_name ,
206
+ "access_token" : access_token ,
207
+ "account_id" : account_id ,
208
+ "account_email" : account_email ,
209
+ "expires_at" : expires_at ,
210
+ "refresh_token" : refresh_token ,
211
+ }
212
+
193
213
try :
194
- user = await self .get_by_oauth_account (
195
- oauth_account .oauth_name , oauth_account .account_id
196
- )
214
+ user = await self .get_by_oauth_account (oauth_name , account_id )
197
215
except UserNotExists :
198
216
try :
199
217
# Link account
200
- user = await self .get_by_email (oauth_account .account_email )
201
- oauth_accounts = [* user .oauth_accounts , oauth_account ]
202
- await self .user_db .update (user , {"oauth_accounts" : oauth_accounts })
218
+ user = await self .get_by_email (account_email )
219
+ user = await self .user_db .add_oauth_account (user , oauth_account_dict )
203
220
except UserNotExists :
204
221
# Create account
205
222
password = self .password_helper .generate ()
206
223
user_dict = {
207
- "email" : oauth_account . account_email ,
224
+ "email" : account_email ,
208
225
"hashed_password" : self .password_helper .hash (password ),
209
- "oauth_accounts" : [oauth_account ],
210
226
}
211
227
user = await self .user_db .create (user_dict )
228
+ user = await self .user_db .add_oauth_account (user , oauth_account_dict )
212
229
await self .on_after_register (user , request )
213
230
else :
214
231
# Update oauth
215
- updated_oauth_accounts = []
216
- for existing_oauth_account in user .oauth_accounts : # type: ignore
232
+ for existing_oauth_account in user .oauth_accounts :
217
233
if (
218
- existing_oauth_account .account_id == oauth_account . account_id
219
- and existing_oauth_account .oauth_name == oauth_account . oauth_name
234
+ existing_oauth_account .account_id == account_id
235
+ and existing_oauth_account .oauth_name == oauth_name
220
236
):
221
- oauth_account .id = existing_oauth_account .id
222
- updated_oauth_accounts .append (oauth_account )
223
- else :
224
- updated_oauth_accounts .append (existing_oauth_account )
225
- await self .user_db .update (user , {"oauth_accounts" : updated_oauth_accounts })
237
+ user = await self .user_db .update_oauth_account (
238
+ user , existing_oauth_account , oauth_account_dict
239
+ )
226
240
227
241
return user
228
242
0 commit comments