File size: 1,191 Bytes
d2726bc |
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 |
from fastapi import Depends
from fastapi.security import OAuth2PasswordBearer
from typing import Annotated
from db.supabase_service import get_supabase
from supabase import Client
from utils.exceptions import BAD_REQUEST, UNAUTHORIZED
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="login")
def get_user_response(token: str, supabase: Client):
try:
return supabase.auth.get_user(token)
except:
raise BAD_REQUEST
async def get_id(
token: Annotated[str, Depends(oauth2_scheme)],
supabase: Annotated[Client, Depends(get_supabase)],
):
try:
res = get_user_response(token, supabase)
if res is None:
raise UNAUTHORIZED
email: str = res.user.id
return email
except:
raise UNAUTHORIZED
async def get_role(
token: Annotated[str, Depends(oauth2_scheme)],
supabase: Annotated[Client, Depends(get_supabase)],
):
try:
res = get_user_response(token, supabase)
if res is None:
raise UNAUTHORIZED
role: str = res.user.user_metadata.get("role", None)
if not role:
raise UNAUTHORIZED
return role
except:
raise UNAUTHORIZED
|