Gregniuki commited on
Commit
e007f86
1 Parent(s): 5606447

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +18 -9
main.py CHANGED
@@ -94,16 +94,25 @@ async def registration_successful(request: Request):
94
 
95
  @app.get("/verify/{verification_token}", response_class=HTMLResponse)
96
  async def verify_email(verification_token: str, request: Request, db: Session = Depends(get_db)):
97
- # Perform email verification
98
- verification_result = await verify_email(verification_token, db)
99
-
100
- # Handle a successful verification
101
- if "message" in verification_result:
102
- # Redirect the user to the protected area
103
- return RedirectResponse("/protected")
 
 
104
 
105
- # Handle any other cases, such as errors
106
- return HTTPException(status_code=400, detail="Verification failed")
 
 
 
 
 
 
 
107
 
108
  # User authentication (protected route)
109
  @app.post("/protected", response_model=str)
 
94
 
95
  @app.get("/verify/{verification_token}", response_class=HTMLResponse)
96
  async def verify_email(verification_token: str, request: Request, db: Session = Depends(get_db)):
97
+ # Verify the email using the token
98
+ user_email = verify_token(verification_token)
99
+ if not user_email:
100
+ return HTTPException(status_code=400, detail="Invalid verification token")
101
+
102
+ # Get the user by email
103
+ user = get_user_by_email(db, user_email)
104
+ if not user:
105
+ return HTTPException(status_code=400, detail="User not found")
106
 
107
+ if user.is_verified:
108
+ return HTTPException(status_code=400, detail="Email already verified")
109
+
110
+ # Mark the email as verified in the database
111
+ user.is_verified = True
112
+ db.commit()
113
+
114
+ # Handle a successful verification
115
+ return RedirectResponse("/protected")
116
 
117
  # User authentication (protected route)
118
  @app.post("/protected", response_model=str)