Gregniuki commited on
Commit
22d2b66
1 Parent(s): b67759d

Update appp.py

Browse files
Files changed (1) hide show
  1. appp.py +24 -15
appp.py CHANGED
@@ -118,21 +118,30 @@ async def register_google(request: Request):
118
 
119
  @app.get("/auth/callback")
120
  async def auth_callback(request: Request, db: Session = Depends(get_db)):
121
- token = await oauth.google.authorize_access_token(request)
122
- user_info = await oauth.google.parse_id_token(request, token)
123
-
124
- existing_user = db.query(User).filter(User.email == user_info['email']).first()
125
- if existing_user:
126
- request.session["user_info"] = {"username": existing_user.username, "email": existing_user.email}
127
- return RedirectResponse(url="/login")
128
- else:
129
- new_user = User(email=user_info['email'], username=user_info.get('name'), is_verified=True)
130
- db.add(new_user)
131
- db.commit()
132
- db.refresh(new_user)
133
- request.session["user_info"] = {"username": new_user.username, "email": new_user.email}
134
-
135
- return RedirectResponse(url="/registration_successful")
 
 
 
 
 
 
 
 
 
136
 
137
  @app.get("/registration_successful", response_class=HTMLResponse)
138
  async def registration_successful(request: Request):
 
118
 
119
  @app.get("/auth/callback")
120
  async def auth_callback(request: Request, db: Session = Depends(get_db)):
121
+ try:
122
+ token = await oauth.google.authorize_access_token(request)
123
+ user_info = await oauth.google.parse_id_token(request, token)
124
+
125
+ existing_user = db.query(User).filter(User.email == user_info['email']).first()
126
+ if existing_user:
127
+ access_token = create_access_token(data={"sub": existing_user.email}, expires_delta=timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES))
128
+ response = RedirectResponse(url="/protected")
129
+ response.set_cookie(key="access_token", value=f"Bearer {access_token}", httponly=True, secure=True, samesite='Lax')
130
+ return response
131
+ else:
132
+ new_user = User(email=user_info['email'], username=user_info.get('name'), is_verified=True)
133
+ db.add(new_user)
134
+ db.commit()
135
+ db.refresh(new_user)
136
+
137
+ access_token = create_access_token(data={"sub": new_user.email}, expires_delta=timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES))
138
+ response = RedirectResponse(url="/protected")
139
+ response.set_cookie(key="access_token", value=f"Bearer {access_token}", httponly=True, secure=True, samesite='Lax')
140
+ return response
141
+ except Exception as e:
142
+ # Handle any exceptions that may occur during the OAuth process
143
+ print(f"OAuth exception: {e}")
144
+ raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="An error occurred during OAuth authentication")
145
 
146
  @app.get("/registration_successful", response_class=HTMLResponse)
147
  async def registration_successful(request: Request):