Update main.py
Browse files
main.py
CHANGED
@@ -13,12 +13,49 @@ import httpx
|
|
13 |
#import auth
|
14 |
#import tts
|
15 |
import os
|
16 |
-
import asyncio
|
|
|
17 |
|
18 |
my_secret_key = os.environ['my_secret_key']
|
19 |
from fastapi.staticfiles import StaticFiles
|
|
|
20 |
|
21 |
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
24 |
#router = APIRouter()
|
|
|
13 |
#import auth
|
14 |
#import tts
|
15 |
import os
|
16 |
+
#import asyncio
|
17 |
+
import authlib
|
18 |
|
19 |
my_secret_key = os.environ['my_secret_key']
|
20 |
from fastapi.staticfiles import StaticFiles
|
21 |
+
from authlib.integrations.starlette_client import OAuth
|
22 |
|
23 |
app = FastAPI()
|
24 |
+
oauth = OAuth()
|
25 |
+
|
26 |
+
# Configure OAuth registry
|
27 |
+
oauth.register(
|
28 |
+
name='google',
|
29 |
+
client_id='YOUR_CLIENT_ID',
|
30 |
+
client_secret='YOUR_CLIENT_SECRET',
|
31 |
+
access_token_url='https://accounts.google.com/o/oauth2/token',
|
32 |
+
authorize_url='https://accounts.google.com/o/oauth2/auth',
|
33 |
+
authorize_params=None,
|
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)):
|
40 |
+
# Exchange code for token
|
41 |
+
token = await oauth.google.authorize_access_token(request)
|
42 |
+
|
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")
|
61 |
#router = APIRouter()
|