Update appp.py
Browse files
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 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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):
|