Gregniuki commited on
Commit
89ee564
1 Parent(s): 1e17d31

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -1
app.py CHANGED
@@ -223,7 +223,37 @@ async def registration_successful(request: Request, db: Session = Depends(get_db
223
 
224
  @app.get("/register", response_class=HTMLResponse)
225
  async def register_get(request: Request):
226
- return templates.TemplateResponse("register.html", {"request": request})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227
 
228
  @app.get("/", response_class=HTMLResponse)
229
  async def landing(request: Request):
 
223
 
224
  @app.get("/register", response_class=HTMLResponse)
225
  async def register_get(request: Request):
226
+ return templates.TemplateResponse("register.html", {"request": request, "google_oauth_url": request.url_for("login_oauth")})
227
+
228
+ @app.post("/register")
229
+ async def register_post(
230
+ request: Request,
231
+ username: str = Form(...),
232
+ email: str = Form(...),
233
+ password: str = Form(...),
234
+ confirm_password: str = Form(...),
235
+ recaptcha_token: str = Form(...),
236
+ db: Session = Depends(get_db)
237
+ ):
238
+ if not await verify_recaptcha(recaptcha_token):
239
+ return templates.TemplateResponse("register.html", {"request": request, "error_message": "reCAPTCHA validation failed."})
240
+
241
+ if password != confirm_password:
242
+ return templates.TemplateResponse("register.html", {"request": request, "error_message": "Passwords do not match."})
243
+
244
+ user_data = UserCreate(username=username, email=email, password=password)
245
+ try:
246
+ registered_user = register_user(user_data, db)
247
+ # Store user info in the session after successful registration
248
+ request.session["user_info"] = {"username": registered_user.username, "email": registered_user.email}
249
+ # Create an access token
250
+ access_token = create_access_token(data={"sub": registered_user.email})
251
+ # Redirect to the protected route
252
+ response = RedirectResponse(url="/protected")
253
+ response.set_cookie(key="access_token", value=f"Bearer {access_token}", httponly=True)
254
+ return response
255
+ except HTTPException as e:
256
+ return templates.TemplateResponse("register.html", {"request": request, "error_message": e.detail})
257
 
258
  @app.get("/", response_class=HTMLResponse)
259
  async def landing(request: Request):