Update main.py
Browse files
main.py
CHANGED
@@ -34,6 +34,11 @@ oauth.register(
|
|
34 |
api_base_url='https://www.googleapis.com/oauth2/v1/',
|
35 |
client_kwargs={'scope': 'openid email profile'}
|
36 |
)
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
@app.get("/auth/callback")
|
39 |
async def auth_callback(request: Request, db: Session = Depends(get_db)):
|
@@ -43,18 +48,17 @@ async def auth_callback(request: Request, db: Session = Depends(get_db)):
|
|
43 |
# Use token to get user info
|
44 |
user_info = await oauth.google.parse_id_token(request, token)
|
45 |
|
46 |
-
#
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
# db.commit()
|
54 |
-
|
55 |
-
# Redirect to a success page, or return a success response
|
56 |
-
return user_info # or redirect to another page
|
57 |
|
|
|
|
|
|
|
58 |
# Other routes and logic for your application...
|
59 |
|
60 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
|
34 |
api_base_url='https://www.googleapis.com/oauth2/v1/',
|
35 |
client_kwargs={'scope': 'openid email profile'}
|
36 |
)
|
37 |
+
@app.get("/login/oauth")
|
38 |
+
async def login_oauth(request: Request):
|
39 |
+
# Redirect to OAuth provider (e.g., Google)
|
40 |
+
redirect_uri = request.url_for('auth_callback')
|
41 |
+
return await oauth.google.authorize_redirect(request, redirect_uri)
|
42 |
|
43 |
@app.get("/auth/callback")
|
44 |
async def auth_callback(request: Request, db: Session = Depends(get_db)):
|
|
|
48 |
# Use token to get user info
|
49 |
user_info = await oauth.google.parse_id_token(request, token)
|
50 |
|
51 |
+
# Check if this user is already in your database, if not, create a new user record
|
52 |
+
db_user = db.query(User).filter(User.email == user_info['email']).first()
|
53 |
+
if not db_user:
|
54 |
+
db_user = User(email=user_info['email'], name=user_info['name'])
|
55 |
+
db.add(db_user)
|
56 |
+
db.commit()
|
57 |
+
db.refresh(db_user)
|
|
|
|
|
|
|
|
|
58 |
|
59 |
+
# Redirect to a success page, or return a success response
|
60 |
+
# Here you can also create a session or token for your user
|
61 |
+
return {"message": "Login successful", "user": {"email": db_user.email, "name": db_user.name}}
|
62 |
# Other routes and logic for your application...
|
63 |
|
64 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|