Gregniuki commited on
Commit
36ba74d
1 Parent(s): 3d97d87

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +15 -11
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
- # Here, you can check if this user is already in your database,
47
- # and if not, create a new user record.
48
- # Example:
49
- # db_user = db.query(YourUserModel).filter(YourUserModel.email == user_info['email']).first()
50
- # if not db_user:
51
- # db_user = YourUserModel(email=user_info['email'], name=user_info['name'])
52
- # db.add(db_user)
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")