Gregniuki commited on
Commit
7236499
1 Parent(s): 9713743

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -19
app.py CHANGED
@@ -405,28 +405,33 @@ def register_user(user_data: UserCreate, db: Session):
405
 
406
  @app.get("/protected", response_class=HTMLResponse)
407
  async def get_protected(
408
- request: Request,
409
  db: Session = Depends(get_db),
410
- token: Optional[str] = None # token is Optional because it may come from the cookie
411
  ):
412
- print(token)
413
- # Try to get the token from the query parameter first, then fall back to the cookie
414
- access_token = token or request.cookies.get("access_token")
415
- print(access_token)
416
- if not access_token:
417
- raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Not authenticated xxx")
418
-
419
- # Here verify_token is used directly in the endpoint
420
- # If the token is invalid, verify_token will raise an HTTPException and the following lines will not be executed
421
- user_email = verify_token(access_token) # Assuming that verify_token returns the user's email if the token is valid
422
-
423
- # Get the user from the database
424
- db_user = get_user_by_email(db, user_email)
425
- if db_user is None or not db_user.is_verified:
426
- raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="User not found or not verified in the database")
427
 
428
- # Render a template response
429
- return templates.TemplateResponse("protected.html", {"request": request, "user": db_user.username})
 
 
 
 
 
 
 
 
 
430
 
431
  def verify_email(verification_token: str, db: Session = Depends(get_db)):
432
  # Verify the email using the token
 
405
 
406
  @app.get("/protected", response_class=HTMLResponse)
407
  async def get_protected(
408
+ request: Request,
409
  db: Session = Depends(get_db),
410
+ authorization: Optional[str] = Header(None) # token from Authorization header
411
  ):
412
+ # Try to get the token from the Authorization header
413
+ if authorization:
414
+ scheme, _, token = authorization.partition(' ')
415
+ if scheme.lower() != 'bearer':
416
+ raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid authentication scheme")
417
+ else:
418
+ # Fall back to the cookie
419
+ token = request.cookies.get("access_token")
420
+
421
+ if not token:
422
+ raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Not authenticated")
 
 
 
 
423
 
424
+ # Verify the token and get user
425
+ try:
426
+ user_email = verify_token(token) # Implement your token verification logic
427
+ db_user = get_user_by_email(db, user_email)
428
+ if db_user is None or not db_user.is_verified:
429
+ raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="User not found or not verified in the database")
430
+
431
+ # Render a template response
432
+ return templates.TemplateResponse("protected.html", {"request": request, "user": db_user.username})
433
+ except Exception as e: # Replace with specific exceptions as per your verification logic
434
+ raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail=str(e))
435
 
436
  def verify_email(verification_token: str, db: Session = Depends(get_db)):
437
  # Verify the email using the token