Skip to content

Commit

Permalink
refactor: profile upsert (#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
RaoHai authored Jan 6, 2025
2 parents 1f184e2 + 1fbca00 commit af08e82
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
8 changes: 4 additions & 4 deletions server/auth/clients/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from abc import abstractmethod
from fastapi import Request
from core.dao.profilesDAO import ProfilesDAO
from utils.random_str import random_str
from petercat_utils import get_client


class BaseAuthClient:
Expand All @@ -29,8 +29,8 @@ def generateAnonymousUser(self, clientId: str) -> tuple[str, dict]:
async def anonymouseLogin(self, request: Request) -> dict:
clientId = request.query_params.get("clientId") or random_str()
token, data = self.generateAnonymousUser(clientId = clientId)
supabase = get_client()
supabase.table("profiles").upsert(data).execute()
profiles_dao = ProfilesDAO()
profiles_dao.upsert_user(data)
request.session["user"] = data
return data

Expand All @@ -49,7 +49,7 @@ async def get_oauth_token(self) -> str:
@abstractmethod
async def get_user_info(self, request: Request) -> dict:
pass

@abstractmethod
async def get_access_token(self, user_id: str, provider="github") -> str:
pass
13 changes: 7 additions & 6 deletions server/auth/clients/local.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import secrets
from fastapi import Request
from fastapi.responses import RedirectResponse
from petercat_utils import get_client, get_env_variable
from core.dao.profilesDAO import ProfilesDAO
from petercat_utils import get_env_variable
from auth.clients.base import BaseAuthClient

PETERCAT_LOCAL_UID = get_env_variable("PETERCAT_LOCAL_UID")
Expand All @@ -13,20 +14,20 @@
class LocalClient(BaseAuthClient):
def __init__(self):
pass

async def login(self, request: Request):
data = await self.get_user_info()
supabase = get_client()
supabase.table("profiles").upsert(data).execute()
profiles_dao = ProfilesDAO()
profiles_dao.upsert_user(data)
request.session["user"] = data

return RedirectResponse(url=f"{WEB_LOGIN_SUCCESS_URL}", status_code=302)

async def logout(self, request: Request, redirect: str):
if redirect:
return RedirectResponse(url=f"{redirect}", status_code=302)
return {"success": True}

async def get_user_info(self, user_id):
token = PETERCAT_LOCAL_UID
username = PETERCAT_LOCAL_UNAME
Expand Down
12 changes: 10 additions & 2 deletions server/core/dao/profilesDAO.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,17 @@ def get_profile(self, user_id: str):
.execute()
)
return resp.data[0] if resp.data else None

def get_agreement_status(self, user_id: str):

def upsert_user(self, user_data: dict):

resp = (
self.client.table("profiles")
.upsert(user_data)
.execute()
)
return resp.data

def get_agreement_status(self, user_id: str):
resp = (
self.client.table("profiles")
.select("agreement_accepted")
Expand Down

0 comments on commit af08e82

Please sign in to comment.