-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathauth0_api.py
More file actions
68 lines (53 loc) · 2.19 KB
/
Copy pathauth0_api.py
File metadata and controls
68 lines (53 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from authlib.integrations.starlette_client import OAuth
from fastapi import APIRouter, Depends, HTTPException, Request
from fastapi.responses import RedirectResponse
from sqlmodel import Session
from src.auth.services import get_or_create_user_by_email # Fix the import path
from src.core.config import settings
from src.core.db import get_db
router = APIRouter(tags=["auth"])
oauth = OAuth()
oauth.register(
name="auth0",
client_id=settings.AUTH0_CLIENT_ID,
client_secret=settings.AUTH0_CLIENT_SECRET,
server_metadata_url=f"https://{settings.AUTH0_DOMAIN}/.well-known/openid-configuration",
client_kwargs={"scope": "openid profile email"},
)
@router.get("/login", name="auth0_login")
async def login(request: Request, redirect_to: str = "/collections"):
request.session["redirect_to"] = redirect_to
redirect_uri = request.url_for("auth0_callback")
return await oauth.auth0.authorize_redirect(
request, redirect_uri, prompt="select_account", connection="google-oauth2"
)
@router.get("/callback", name="auth0_callback")
async def auth0_callback(request: Request, db: Session = Depends(get_db)):
# Exchange code for token
token = await oauth.auth0.authorize_access_token(request)
# Extract user info
user_info = token.get("userinfo")
if not user_info:
user_info = await oauth.auth0.userinfo(token=token)
if not user_info or "email" not in user_info:
raise HTTPException(status_code=400, detail="Invalid user info from Auth0")
# Create or get user in local DB
db_user = get_or_create_user_by_email(
session=db,
email=user_info["email"],
defaults={
"auth0_id": user_info["sub"],
"full_name": user_info.get("name"),
"is_active": True,
},
)
# Store user in session
request.session["user_id"] = str(db_user.id)
# Determine redirect target
redirect_to = request.session.pop("redirect_to", "/collections")
redirect_url = f"{settings.FRONTEND_URL}{redirect_to}"
return RedirectResponse(url=redirect_url)
@router.get("/logout", name="auth0_logout")
async def logout(request: Request):
request.session.clear()
return {"detail": "Logged out"}