1212
1313SESSION_EXPIRY_MINUTES = 5
1414
15- async def get_or_create_user_by_discord (discord_id : str , display_name : str , discord_username : str , avatar_url : Optional [str ]) -> User :
15+ async def get_or_create_user_by_discord (
16+ discord_id : str , display_name : str , discord_username : str , avatar_url : Optional [str ]
17+ ) -> User :
1618 """
1719 Get or create a user by Discord ID.
1820 """
@@ -88,7 +90,9 @@ async def create_verification_session(discord_id: str) -> Optional[str]:
8890 logger .error (f"Error creating verification session for Discord ID { discord_id } : { str (e )} " )
8991 return None
9092
91- async def find_user_by_session_and_verify (session_id : str , github_id : str , github_username : str , email : Optional [str ]) -> Optional [User ]:
93+ async def find_user_by_session_and_verify (
94+ session_id : str , github_id : str , github_username : str , email : Optional [str ]
95+ ) -> Optional [User ]:
9296 """
9397 Find and verify user using session ID with expiry validation.
9498 """
@@ -104,23 +108,28 @@ async def find_user_by_session_and_verify(session_id: str, github_id: str, githu
104108
105109 discord_id , expiry_time = session_data
106110
107- if datetime .now () > expiry_time :
108- logger .warning (f"Verification session { session_id } has expired" )
109- del _verification_sessions [session_id ]
110- return None
111-
112- del _verification_sessions [session_id ]
113-
114111 current_time = datetime .now ().isoformat ()
115- user_res = await supabase .table ("users" ).select ("*" ).eq ("discord_id" , discord_id ).neq ("verification_token" , None ).gt ("verification_token_expires_at" , current_time ).limit (1 ).execute ()
112+ user_res = await supabase .table ("users" ).select ("*" ).eq (
113+ "discord_id" , discord_id
114+ ).neq (
115+ "verification_token" , None
116+ ).gt (
117+ "verification_token_expires_at" , current_time
118+ ).limit (1 ).execute ()
116119
117120 if not user_res .data :
118- logger .warning (f"No valid pending verification found for Discord ID: { discord_id } " )
121+ logger .warning (f"No valid pending verification found for Discord ID: { discord_id } (token may have expired)" )
122+ del _verification_sessions [session_id ]
119123 return None
120124
125+ # Delete the session after successful validation
126+ del _verification_sessions [session_id ]
127+
121128 user_to_verify = user_res .data [0 ]
122129
123- existing_github_user = await supabase .table ("users" ).select ("*" ).eq ("github_id" , github_id ).neq ("id" , user_to_verify ['id' ]).limit (1 ).execute ()
130+ existing_github_user = await supabase .table ("users" ).select ("*" ).eq (
131+ "github_id" , github_id
132+ ).neq ("id" , user_to_verify ['id' ]).limit (1 ).execute ()
124133 if existing_github_user .data :
125134 logger .warning (f"GitHub account { github_username } is already linked to another user" )
126135 await supabase .table ("users" ).update ({
0 commit comments