88import logging
99from typing import Annotated
1010
11- from fastapi import Cookie , Depends , HTTPException
11+ from fastapi import Depends , HTTPException
1212from fastapi .security import OAuth2PasswordBearer
13- from gotrue import User
1413from gotrue .errors import AuthApiError
1514from supabase_py_async import AsyncClient , create_client
1615from supabase_py_async .lib .client_options import ClientOptions
1716
1817from app .core .config import settings
19- from app .schemas import Token
18+ from app .schemas .auth import UserIn
19+
20+ super_client : AsyncClient | None = None
21+
22+
23+ async def init_super_client () -> None :
24+ """for validation access_token init at life span event"""
25+ global super_client
26+ super_client = await create_client (
27+ settings .SUPABASE_URL ,
28+ settings .SUPABASE_KEY ,
29+ options = ClientOptions (postgrest_client_timeout = 10 , storage_client_timeout = 10 ),
30+ )
31+ # await super_client.auth.sign_in_with_password(
32+ # {"email": settings.SUPERUSER_EMAIL, "password": settings.SUPERUSER_PASSWORD}
33+ # )
34+
2035
2136# auto get access_token from header
2237reusable_oauth2 = OAuth2PasswordBearer (
2338 tokenUrl = "please login by supabase-js to get token"
2439)
2540AccessTokenDep = Annotated [str , Depends (reusable_oauth2 )]
26- RefreshTokenDep = Annotated [str | None , Cookie ()]
2741
2842
29- async def get_token (
30- access_token : AccessTokenDep , refresh_token : RefreshTokenDep
31- ) -> Token | None :
32- if not access_token :
33- raise HTTPException (status_code = 401 , detail = "No access token" )
34- if not refresh_token :
35- raise HTTPException (status_code = 401 , detail = "No refresh token" )
36- return Token (access_token = access_token , refresh_token = refresh_token )
43+ async def get_current_user (access_token : AccessTokenDep ) -> UserIn :
44+ """get current user from access_token and validate same time"""
45+ if not super_client :
46+ raise HTTPException (status_code = 500 , detail = "Super client not initialized" )
47+
48+ user_rsp = await super_client .auth .get_user (jwt = access_token )
49+ if not user_rsp :
50+ logging .error ("User not found" )
51+ raise HTTPException (status_code = 404 , detail = "User not found" )
52+ return UserIn (** user_rsp .user .model_dump (), access_token = access_token )
3753
3854
39- TokenDep = Annotated [Token | None , Depends (get_token )]
55+ CurrentUser = Annotated [UserIn , Depends (get_current_user )]
4056
4157
42- async def get_db (token : TokenDep ) -> AsyncClient :
58+ async def get_db (user : CurrentUser ) -> AsyncClient :
4359 client : AsyncClient | None = None
4460 try :
4561 client = await create_client (
4662 settings .SUPABASE_URL ,
4763 settings .SUPABASE_KEY ,
64+ access_token = user .access_token ,
4865 options = ClientOptions (
4966 postgrest_client_timeout = 10 , storage_client_timeout = 10
5067 ),
5168 )
5269 # checks all done in supabase-py !
53- if not token :
54- raise HTTPException (status_code = 401 , detail = "Invalid authentication" )
55- await client .auth .set_session (token .access_token , token .refresh_token )
70+ # await client.auth.set_session(token.access_token, token.refresh_token)
5671 # session = await client.auth.get_session()
5772 yield client
5873
@@ -67,14 +82,3 @@ async def get_db(token: TokenDep) -> AsyncClient:
6782
6883
6984SessionDep = Annotated [AsyncClient , Depends (get_db )]
70-
71-
72- async def get_current_user (session : SessionDep ) -> User :
73- user = await session .auth .get_user ()
74- if not user :
75- logging .error ("User not found" )
76- raise HTTPException (status_code = 404 , detail = "User not found" )
77- return user
78-
79-
80- CurrentUser = Annotated [User , Depends (get_current_user )]
0 commit comments