ChandimaPrabath commited on
Commit
8ba99a8
·
1 Parent(s): 5b4d7da

1.0.1 debug

Browse files
Files changed (1) hide show
  1. main.py +27 -2
main.py CHANGED
@@ -14,7 +14,7 @@ from typing import Optional
14
  load_dotenv()
15
 
16
  SERVER_NAME = "Nexus Authentication Service"
17
- VERSION = "1.0"
18
 
19
  # Supabase Configuration
20
  SUPABASE_URL = os.getenv("SUPABASE_URL")
@@ -164,11 +164,31 @@ async def login(request: LoginRequest, user_agent: str = Header(...)):
164
 
165
  @auth_router.post("/logout")
166
  async def logout(user_id: str, token: str):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
  supabase.table("sessions").delete().eq("user_id", user_id).eq("token", token).execute()
 
168
  return {"message": "Session forcefully expired"}
169
 
 
170
  @auth_router.get("/validate", response_model=TokenResponse)
171
  async def validate_token(user_id: str, token: str, user_agent: str = Header(...)):
 
172
  session_query = (
173
  supabase.table("sessions")
174
  .select("*")
@@ -178,20 +198,25 @@ async def validate_token(user_id: str, token: str, user_agent: str = Header(...)
178
  .execute()
179
  )
180
 
 
181
  if not session_query.data:
182
  raise HTTPException(
183
  status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token"
184
  )
185
 
186
  session = session_query.data[0]
 
 
187
  if is_token_expired(datetime.fromisoformat(session["expires"])):
188
- supabase.table("sessions").delete().eq("id", session["id"]).execute()
 
189
  raise HTTPException(
190
  status_code=status.HTTP_401_UNAUTHORIZED, detail="Token expired"
191
  )
192
 
193
  return TokenResponse(access_token=token)
194
 
 
195
  @auth_router.get("/search-users", response_model=List[str])
196
  async def search_users(query: str):
197
  users = supabase.table("users").select("username").ilike("username", f"%{query}%").execute()
 
14
  load_dotenv()
15
 
16
  SERVER_NAME = "Nexus Authentication Service"
17
+ VERSION = "1.0.1 debug"
18
 
19
  # Supabase Configuration
20
  SUPABASE_URL = os.getenv("SUPABASE_URL")
 
164
 
165
  @auth_router.post("/logout")
166
  async def logout(user_id: str, token: str):
167
+ # Query to check if the session exists for the given user_id and token
168
+ session_query = (
169
+ supabase.table("sessions")
170
+ .select("*")
171
+ .eq("user_id", user_id)
172
+ .eq("token", token)
173
+ .execute()
174
+ )
175
+
176
+ # If session is not found, raise an unauthorized error
177
+ if not session_query.data:
178
+ raise HTTPException(
179
+ status_code=status.HTTP_401_UNAUTHORIZED,
180
+ detail="Session not found or already expired"
181
+ )
182
+
183
+ # Delete the session using the composite key (user_id and token)
184
  supabase.table("sessions").delete().eq("user_id", user_id).eq("token", token).execute()
185
+
186
  return {"message": "Session forcefully expired"}
187
 
188
+
189
  @auth_router.get("/validate", response_model=TokenResponse)
190
  async def validate_token(user_id: str, token: str, user_agent: str = Header(...)):
191
+ # Query to validate session by user_id, token, and device
192
  session_query = (
193
  supabase.table("sessions")
194
  .select("*")
 
198
  .execute()
199
  )
200
 
201
+ # If no session found, raise unauthorized error
202
  if not session_query.data:
203
  raise HTTPException(
204
  status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token"
205
  )
206
 
207
  session = session_query.data[0]
208
+
209
+ # Check if token is expired
210
  if is_token_expired(datetime.fromisoformat(session["expires"])):
211
+ # Delete session using both user_id and token (composite key)
212
+ supabase.table("sessions").delete().eq("user_id", user_id).eq("token", token).execute()
213
  raise HTTPException(
214
  status_code=status.HTTP_401_UNAUTHORIZED, detail="Token expired"
215
  )
216
 
217
  return TokenResponse(access_token=token)
218
 
219
+
220
  @auth_router.get("/search-users", response_model=List[str])
221
  async def search_users(query: str):
222
  users = supabase.table("users").select("username").ilike("username", f"%{query}%").execute()