@@ -463,15 +463,8 @@ async def oauth_callback(
463
463
}
464
464
465
465
user = await self .user_db .create (user_dict )
466
-
467
- # Add OAuth account only if user creation was successful
468
- if user is not None :
469
- await self .user_db .add_oauth_account (user , oauth_account_dict )
470
- await self .on_after_register (user , request )
471
- else :
472
- raise HTTPException (
473
- status_code = 500 , detail = "Failed to create user account"
474
- )
466
+ await self .user_db .add_oauth_account (user , oauth_account_dict )
467
+ await self .on_after_register (user , request )
475
468
476
469
else :
477
470
# User exists, update OAuth account if needed
@@ -489,12 +482,6 @@ async def oauth_callback(
489
482
oauth_account_dict ,
490
483
)
491
484
492
- # Ensure user is not None before proceeding
493
- if user is None :
494
- raise HTTPException (
495
- status_code = 500 , detail = "Failed to authenticate or create user"
496
- )
497
-
498
485
# NOTE: Most IdPs have very short expiry times, and we don't want to force the user to
499
486
# re-authenticate that frequently, so by default this is disabled
500
487
if expires_at and TRACK_EXTERNAL_IDP_EXPIRY :
@@ -505,14 +492,20 @@ async def oauth_callback(
505
492
506
493
# Handle case where user has used product outside of web and is now creating an account through web
507
494
if not user .role .is_web_login ():
495
+ # We must use the existing user in the session if it matches
496
+ # the user we just got by email/oauth
497
+ if user .id :
498
+ user_by_session = await db_session .get (User , user .id )
499
+ if user_by_session :
500
+ user = user_by_session
501
+
508
502
await self .user_db .update (
509
503
user ,
510
504
{
511
505
"is_verified" : is_verified_by_default ,
512
506
"role" : UserRole .BASIC ,
513
507
},
514
508
)
515
- user .is_verified = is_verified_by_default
516
509
517
510
# this is needed if an organization goes from `TRACK_EXTERNAL_IDP_EXPIRY=true` to `false`
518
511
# otherwise, the oidc expiry will always be old, and the user will never be able to login
0 commit comments