1- from fastapi import APIRouter , Depends , HTTPException , status
1+ from fastapi import APIRouter , Depends , HTTPException , status , Request
22from fastapi .security import OAuth2PasswordBearer , OAuth2PasswordRequestForm
33from sqlmodel .ext .asyncio .session import AsyncSession
44import jwt
1111
1212oauth2_scheme = OAuth2PasswordBearer (tokenUrl = "/authentication/token" )
1313
14- def setup (get_db_session_dep ):
14+ def setup ():
1515 router = APIRouter (prefix = '/authentication' , tags = ['authentication' ])
16- async def authenticate_community (username : str , password : str , session : AsyncSession = Depends ( get_db_session_dep ) ):
16+ async def authenticate_community (request : Request , username : str , password : str ):
1717 # Valida se o usuário existe e se a senha está correta
1818 found_community = await get_community_by_username (
1919 username = username ,
20- session = session
20+ session = request . app . db_session_factory
2121 )
2222 if not found_community or not auth .verify_password (password , found_community .password ):
2323 return None
@@ -27,20 +27,21 @@ async def authenticate_community(username: str, password: str, session: AsyncSes
2727 #### Teste
2828
2929 @router .post ("/create_commumity" )
30- async def create_community ( session : AsyncSession = Depends ( get_db_session_dep ) ):
30+ async def create_community (request : Request ):
3131 password = "123Asd!@#"
3232 hashed_password = auth .hash_password (password )
3333 community = DBCommunity (username = "username" , email = "username@test.com" , password = hashed_password )
34+ session = request .app .db_session_factory
3435 session .add (community )
3536 await session .commit ()
3637 await session .refresh (community )
3738 return {'msg' :'succes? ' }
3839 #### Teste
3940
4041 @router .post ("/token" , response_model = Token )
41- async def login_for_access_token (form_data : OAuth2PasswordRequestForm = Depends (), session : AsyncSession = Depends (get_db_session_dep ) ) :
42+ async def login_for_access_token (request : Request , form_data : OAuth2PasswordRequestForm = Depends () ) :
4243 # Rota de login: valida credenciais e retorna token JWT
43- community = await authenticate_community (form_data .username , form_data .password , session )
44+ community = await authenticate_community (form_data .username , form_data .password , request . app . db_session_factory )
4445 if not community :
4546 raise HTTPException (
4647 status_code = status .HTTP_401_UNAUTHORIZED ,
0 commit comments