Skip to content

Commit

Permalink
fix: catch user info is null
Browse files Browse the repository at this point in the history
  • Loading branch information
RaoHai committed Aug 18, 2024
1 parent da21415 commit 02cc6c9
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
4 changes: 3 additions & 1 deletion server/auth/get_user_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async def getUserInfoByToken(token):
}
return data
else :
return {}
return None

async def getUserAccessToken(user_id: str, provider = 'github'):
token = await get_oauth_token()
Expand Down Expand Up @@ -67,6 +67,8 @@ async def get_user_access_token(petercat_user_token: Annotated[str | None, Cooki
if petercat_user_token is None:
return None
user_info = await getUserInfoByToken(petercat_user_token)
if user_info is None:
return None
access_token = await getUserAccessToken(user_id=user_info['id'])
print(f"get_user_access_token: user_info={user_info}, access_token={access_token}")
return access_token
2 changes: 2 additions & 0 deletions server/routers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ async def callback(request: Request, response: Response):
if not token:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Missing authorization token")
data = await getUserInfoByToken(token)
if data is None:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Missing authorization token")
supabase = get_client()
supabase.table("profiles").upsert(data).execute()
print(f"auth_callback: {data}, token={token}")
Expand Down
2 changes: 0 additions & 2 deletions server/tools/bot_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from langchain.tools import tool
from github import Github
from petercat_utils import get_client
from prompts.bot_template import generate_prompt_by_repo_name
from auth.get_user_info import getUserInfoByToken
from bot.builder import bot_builder

g = Github()
Expand Down
6 changes: 6 additions & 0 deletions server/verify/rate_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ async def verify_rate_limit(petercat: str = Cookie(None)):
if not petercat:
raise HTTPException(status_code=403, detail="Must Login")
user = await getUserInfoByToken(petercat)
if user is None:
raise HTTPException(
status_code=429,
detail="Rate Limit Exceeded, Try It Later",
headers={"Retry-After": "60"}
)
user_id = user["id"]
supabase = get_client()
table = supabase.table("user_token_usage")
Expand Down

0 comments on commit 02cc6c9

Please sign in to comment.