@@ -484,19 +484,29 @@ def generate_oauth_blueprints():
484484 ub .session_commit ("{} Blueprint Created" .format (provider ))
485485
486486 oauth_ids = ub .session .query (ub .OAuthProvider ).filter (ub .OAuthProvider .provider_name .in_ (['github' , 'google' ])).all ()
487+
488+ # Ensure deterministic assignment of providers regardless of DB query order
489+ github_provider = next ((p for p in oauth_ids if p .provider_name == 'github' ), None )
490+ google_provider = next ((p for p in oauth_ids if p .provider_name == 'google' ), None )
491+
492+ # Fallback if providers are missing (shouldn't happen due to creation logic above)
493+ if not github_provider or not google_provider :
494+ log .error ("OAuth providers missing after creation check" )
495+ return []
496+
487497 ele1 = dict (provider_name = 'github' ,
488- id = oauth_ids [ 0 ] .id ,
489- active = oauth_ids [ 0 ] .active ,
490- oauth_client_id = oauth_ids [ 0 ] .oauth_client_id ,
498+ id = github_provider .id ,
499+ active = github_provider .active ,
500+ oauth_client_id = github_provider .oauth_client_id ,
491501 scope = None ,
492- oauth_client_secret = oauth_ids [ 0 ] .oauth_client_secret ,
502+ oauth_client_secret = github_provider .oauth_client_secret ,
493503 obtain_link = 'https://github.com/settings/developers' )
494504 ele2 = dict (provider_name = 'google' ,
495- id = oauth_ids [ 1 ] .id ,
496- active = oauth_ids [ 1 ] .active ,
505+ id = google_provider .id ,
506+ active = google_provider .active ,
497507 scope = ["https://www.googleapis.com/auth/userinfo.email" ],
498- oauth_client_id = oauth_ids [ 1 ] .oauth_client_id ,
499- oauth_client_secret = oauth_ids [ 1 ] .oauth_client_secret ,
508+ oauth_client_id = google_provider .oauth_client_id ,
509+ oauth_client_secret = google_provider .oauth_client_secret ,
500510 obtain_link = 'https://console.developers.google.com/apis/credentials' )
501511 oauthblueprints .append (ele1 )
502512 oauthblueprints .append (ele2 )
@@ -781,12 +791,9 @@ def github_login():
781791 del github .token
782792 except Exception :
783793 pass
784- # Force clear session to prevent loops
785- if 'github_oauth_token' in session : session .pop ('github_oauth_token' , None )
786- if 'github_oauth_user_id' in session : session .pop ('github_oauth_user_id' , None )
787-
788794 flash (_ ("GitHub Oauth error: {}" ).format (e ), category = "error" )
789795 log .error (e )
796+ return redirect (url_for ('github.login' ))
790797 return redirect (url_for ('web.login' ))
791798
792799
@@ -813,12 +820,9 @@ def google_login():
813820 del google .token
814821 except Exception :
815822 pass
816- # Force clear session to prevent loops
817- if 'google_oauth_token' in session : session .pop ('google_oauth_token' , None )
818- if 'google_oauth_user_id' in session : session .pop ('google_oauth_user_id' , None )
819-
820823 flash (_ ("Google Oauth error: {}" ).format (e ), category = "error" )
821824 log .error (e )
825+ return redirect (url_for ("google.login" ))
822826 return redirect (url_for ('web.login' ))
823827
824828
@@ -841,10 +845,6 @@ def generic_login():
841845 del oauthblueprints [2 ]['blueprint' ].token
842846 except Exception :
843847 pass
844- # Force clear session to prevent loops
845- if 'generic_oauth_token' in session : session .pop ('generic_oauth_token' , None )
846- if 'generic_oauth_user_id' in session : session .pop ('generic_oauth_user_id' , None )
847-
848848 flash (_ ("OAuth error: {}" ).format (e ), category = "error" )
849849 log .error (e )
850850 return redirect (url_for ("generic.login" ))
@@ -853,12 +853,9 @@ def generic_login():
853853 del oauthblueprints [2 ]['blueprint' ].token
854854 except Exception :
855855 pass
856- # Force clear session to prevent loops
857- if 'generic_oauth_token' in session : session .pop ('generic_oauth_token' , None )
858- if 'generic_oauth_user_id' in session : session .pop ('generic_oauth_user_id' , None )
859-
860856 flash (_ ("OAuth error: {}" ).format (e ), category = "error" )
861857 log .error (e )
858+ return redirect (url_for ("generic.login" ))
862859 return redirect (url_for ("web.login" ))
863860
864861
0 commit comments