Gregniuki commited on
Commit
0872457
1 Parent(s): 584f738

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -0
app.py CHANGED
@@ -237,6 +237,27 @@ async def verify_recaptcha(recaptcha_token: str) -> bool:
237
 
238
  return recaptcha_result.get('success', False)
239
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
240
  @app.get("/register", response_class=HTMLResponse)
241
  async def register_get(request: Request):
242
  return templates.TemplateResponse("register.html", {"request": request, "google_oauth_url": request.url_for("login_oauth")})
 
237
 
238
  return recaptcha_result.get('success', False)
239
 
240
+ @app.get("/verify", response_class=HTMLResponse)
241
+ async def verify_email(token: str, db: Session = Depends(get_db)):
242
+ user = get_user_by_verification_token(db, token)
243
+ if not user:
244
+ raise HTTPException(status_code=400, detail="Invalid verification token")
245
+
246
+ if user.is_verified:
247
+ raise HTTPException(status_code=400, detail="Email already verified")
248
+
249
+ user.is_verified = True
250
+ user.email_verification_token = None # Clear the verification token
251
+ db.commit()
252
+
253
+ # Create access token for the user after successful verification
254
+ access_token = create_access_token(data={"sub": user.email}, expires_delta=timedelta(minutes=auth_views.ACCESS_TOKEN_EXPIRE_MINUTES))
255
+
256
+ # Redirect to the protected route and set the token in a secure, HTTP-only cookie
257
+ response = RedirectResponse(url="/protected")
258
+ response.set_cookie(key="access_token", value=f"Bearer {access_token}", httponly=True, secure=True, samesite='Lax')
259
+ return response
260
+
261
  @app.get("/register", response_class=HTMLResponse)
262
  async def register_get(request: Request):
263
  return templates.TemplateResponse("register.html", {"request": request, "google_oauth_url": request.url_for("login_oauth")})