fixed

parent beb6f38d
import fastapi
from fastapi.security import OAuth2PasswordRequestForm
from schemas import CreateUserSchema, CreatedUserResponseSchema, RefreshTokenSchema
from schemas import CreateUserSchema, CreatedUserResponseSchema, RefreshTokenSchema, AccessTokenSchema
from ctrl import UserController
import services
......@@ -25,16 +25,16 @@ async def login(
return await services.auth_service.login(form_data)
@user_router.get('/me')
async def read_user_me(
user: dict = fastapi.Depends(services.auth_service.get_current_user)
@auth_router.post('/check_token')
async def check_token(
data: AccessTokenSchema,
):
return user
response = await services.auth_service.check_token(data.token)
return response
@auth_router.post('/refresh')
async def refresh_token(
data: RefreshTokenSchema,
user: dict = fastapi.Depends(services.auth_service.get_current_user)
):
return services.token_service.refresh_token(data.refresh_token)
......@@ -34,3 +34,7 @@ class TokenData:
class LoginRequestModel:
username: str
password: str
class AccessTokenSchema(BaseModel):
token: str
......@@ -5,6 +5,7 @@ import fastapi.security as fs
from jose import jwt, JWTError
from passlib.context import CryptContext
import schemas
import const
from repository import UserRepository
......@@ -85,9 +86,18 @@ class AuthService:
return token_service.create_tokens(schemas.TokenData(sub=user.id))
async def get_current_user(self, token: str = fa.Security(oauth2_scheme)):
token_data = token_service.decode_token(token)
return await self.user_repo.get(id=token_data.sub)
async def check_token(self, token: str):
try:
token_data = token_service.decode_token(token)
user = await auth_service.user_repo.get(id=token_data.sub)
if user is None:
return {'success': False}
return {'success': True}
except fa.HTTPException:
return {'success': False}
except Exception:
return {'success': False}
auth_service = AuthService()
......@@ -16,7 +16,7 @@ services:
- .env
command: bash -c "python3 code/main.py"
db:
auth-db:
image: postgres:14-alpine
container_name: auth-pg
env_file:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment