fixed

parent beb6f38d
import fastapi import fastapi
from fastapi.security import OAuth2PasswordRequestForm from fastapi.security import OAuth2PasswordRequestForm
from schemas import CreateUserSchema, CreatedUserResponseSchema, RefreshTokenSchema from schemas import CreateUserSchema, CreatedUserResponseSchema, RefreshTokenSchema, AccessTokenSchema
from ctrl import UserController from ctrl import UserController
import services import services
...@@ -25,16 +25,16 @@ async def login( ...@@ -25,16 +25,16 @@ async def login(
return await services.auth_service.login(form_data) return await services.auth_service.login(form_data)
@user_router.get('/me') @auth_router.post('/check_token')
async def read_user_me( async def check_token(
user: dict = fastapi.Depends(services.auth_service.get_current_user) data: AccessTokenSchema,
): ):
return user response = await services.auth_service.check_token(data.token)
return response
@auth_router.post('/refresh') @auth_router.post('/refresh')
async def refresh_token( async def refresh_token(
data: RefreshTokenSchema, data: RefreshTokenSchema,
user: dict = fastapi.Depends(services.auth_service.get_current_user)
): ):
return services.token_service.refresh_token(data.refresh_token) return services.token_service.refresh_token(data.refresh_token)
...@@ -34,3 +34,7 @@ class TokenData: ...@@ -34,3 +34,7 @@ class TokenData:
class LoginRequestModel: class LoginRequestModel:
username: str username: str
password: str password: str
class AccessTokenSchema(BaseModel):
token: str
...@@ -5,6 +5,7 @@ import fastapi.security as fs ...@@ -5,6 +5,7 @@ import fastapi.security as fs
from jose import jwt, JWTError from jose import jwt, JWTError
from passlib.context import CryptContext from passlib.context import CryptContext
import schemas import schemas
import const import const
from repository import UserRepository from repository import UserRepository
...@@ -85,9 +86,18 @@ class AuthService: ...@@ -85,9 +86,18 @@ class AuthService:
return token_service.create_tokens(schemas.TokenData(sub=user.id)) return token_service.create_tokens(schemas.TokenData(sub=user.id))
async def get_current_user(self, token: str = fa.Security(oauth2_scheme)): async def check_token(self, token: str):
try:
token_data = token_service.decode_token(token) token_data = token_service.decode_token(token)
return await self.user_repo.get(id=token_data.sub)
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() auth_service = AuthService()
...@@ -16,7 +16,7 @@ services: ...@@ -16,7 +16,7 @@ services:
- .env - .env
command: bash -c "python3 code/main.py" command: bash -c "python3 code/main.py"
db: auth-db:
image: postgres:14-alpine image: postgres:14-alpine
container_name: auth-pg container_name: auth-pg
env_file: 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