Gregniuki commited on
Commit
3391487
·
1 Parent(s): b4e60c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -0
app.py CHANGED
@@ -197,6 +197,30 @@ async def auth_callback(request: Request, db: Session = Depends(get_db)):
197
  # Redirect to a success or dashboard page
198
  return RedirectResponse(url="/registration_successful")
199
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200
  @app.get("/", response_class=HTMLResponse)
201
  async def landing(request: Request):
202
  return templates.TemplateResponse("landing.html", {"request": request})
 
197
  # Redirect to a success or dashboard page
198
  return RedirectResponse(url="/registration_successful")
199
 
200
+ @app.get("/registration_successful", response_class=HTMLResponse)
201
+ async def registration_successful(request: Request, db: Session = Depends(get_db)):
202
+ # Assuming the OAuth process has been completed and user info is stored in the session or a similar mechanism
203
+ user_info = request.session.get("user_info") # Replace with your method of retrieving user info
204
+
205
+ if not user_info:
206
+ raise HTTPException(status_code=401, detail="User not authenticated")
207
+
208
+ email = user_info["email"]
209
+ db_user = db.query(User).filter(User.email == email).first()
210
+ if not db_user:
211
+ raise HTTPException(status_code=404, detail="User not found")
212
+
213
+ # Create an access token for the user
214
+ access_token = create_access_token(
215
+ data={"sub": db_user.email},
216
+ expires_delta=timedelta(minutes=auth_views.ACCESS_TOKEN_EXPIRE_MINUTES)
217
+ )
218
+
219
+ # Redirect the user to the protected route
220
+ response = RedirectResponse(url="/protected")
221
+ response.set_cookie(key="access_token", value=f"Bearer {access_token}", httponly=True)
222
+ return response
223
+
224
  @app.get("/", response_class=HTMLResponse)
225
  async def landing(request: Request):
226
  return templates.TemplateResponse("landing.html", {"request": request})