Skip to content

Commit

Permalink
feat: support locally auth
Browse files Browse the repository at this point in the history
  • Loading branch information
RaoHai committed Dec 17, 2024
1 parent 6d2e401 commit c4dbcb5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
11 changes: 4 additions & 7 deletions server/auth/get_user_info.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
from fastapi import Request
from fastapi import Depends, Request
from auth.clients import get_auth_client
from auth.clients.base import BaseAuthClient
from core.models.user import User

from petercat_utils import get_env_variable

AUTH0_DOMAIN = get_env_variable("AUTH0_DOMAIN")

async def getUserAccessToken(user_id: str, provider="github"):
auth_client = get_auth_client()
return await auth_client.get_access_token(user_id=user_id, provider=provider)

async def get_user_id(request: Request):
user_info = request.session.get("user")
try:
Expand All @@ -21,13 +18,13 @@ async def get_user_id(request: Request):
return None


async def get_user(request: Request) -> User | None:
async def get_user(request: Request, auth_client: BaseAuthClient = Depends(get_auth_client)) -> User | None:
user_info = request.session.get("user")
if user_info is None:
return None

if user_info["sub"].startswith("client|"):
return User(**user_info, anonymous=True)

access_token = await getUserAccessToken(user_id=user_info["sub"])
access_token = await auth_client.get_access_token(user_id=user_info["sub"])
return User(**user_info, access_token=access_token, anonymous=False)
6 changes: 2 additions & 4 deletions server/auth/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ async def logout(request: Request):
@router.get("/callback")
async def callback(request: Request, auth_client: BaseAuthClient = Depends(get_auth_client)):
user_info = await auth_client.get_user_info(request)
print(f"user_info={user_info}")
if user_info:
request.session["user"] = dict(user_info)
supabase = get_client()
Expand Down Expand Up @@ -96,12 +95,11 @@ async def bot_generator(


@router.get("/repos")
async def get_user_repos(user_id: Optional[str] = Depends(get_user_id)):
async def get_user_repos(user_id: Optional[str] = Depends(get_user_id), auth_client: BaseAuthClient = Depends(get_auth_client)):
if not user_id:
raise HTTPException(status_code=401, detail="User not found")
try:
client = get_auth_client()
access_token = await client.get_access_token(user_id=user_id)
access_token = await auth_client.get_access_token(user_id=user_id)
g = Github(access_token)
user = g.get_user()
repos = user.get_repos()
Expand Down

0 comments on commit c4dbcb5

Please sign in to comment.