Update app.py
Browse files
app.py
CHANGED
@@ -317,6 +317,9 @@ def create_access_token(data: dict, expires_delta: timedelta = timedelta(minutes
|
|
317 |
return encoded_jwt
|
318 |
|
319 |
def verify_token(token: str = Depends(oauth2_scheme)):
|
|
|
|
|
|
|
320 |
try:
|
321 |
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
322 |
user_email = payload.get("sub")
|
@@ -325,7 +328,9 @@ def verify_token(token: str = Depends(oauth2_scheme)):
|
|
325 |
return user_email
|
326 |
except jwt.ExpiredSignatureError:
|
327 |
raise HTTPException(status_code=401, detail="Token has expired")
|
328 |
-
except jwt.PyJWTError:
|
|
|
|
|
329 |
raise HTTPException(status_code=401, detail="Could not validate credentials")
|
330 |
|
331 |
def validate_token(token: str):
|
@@ -405,6 +410,7 @@ async def get_protected(
|
|
405 |
|
406 |
# Render a template response
|
407 |
return templates.TemplateResponse("protected.html", {"request": request, "user": db_user.username})
|
|
|
408 |
def verify_email(verification_token: str, db: Session = Depends(get_db)):
|
409 |
# Verify the email using the token
|
410 |
user = get_user_by_verification_token(db, verification_token)
|
|
|
317 |
return encoded_jwt
|
318 |
|
319 |
def verify_token(token: str = Depends(oauth2_scheme)):
|
320 |
+
if token.startswith("Bearer "):
|
321 |
+
token = token.split(" ")[1] # Strip the 'Bearer ' prefix if it exists
|
322 |
+
|
323 |
try:
|
324 |
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
325 |
user_email = payload.get("sub")
|
|
|
328 |
return user_email
|
329 |
except jwt.ExpiredSignatureError:
|
330 |
raise HTTPException(status_code=401, detail="Token has expired")
|
331 |
+
except jwt.PyJWTError as e:
|
332 |
+
# Log the error for debugging
|
333 |
+
print(f"JWT decoding error: {e}")
|
334 |
raise HTTPException(status_code=401, detail="Could not validate credentials")
|
335 |
|
336 |
def validate_token(token: str):
|
|
|
410 |
|
411 |
# Render a template response
|
412 |
return templates.TemplateResponse("protected.html", {"request": request, "user": db_user.username})
|
413 |
+
|
414 |
def verify_email(verification_token: str, db: Session = Depends(get_db)):
|
415 |
# Verify the email using the token
|
416 |
user = get_user_by_verification_token(db, verification_token)
|