Update main.py
Browse files
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 |
-
#
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
|
|
|
|
104 |
|
105 |
-
|
106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|