feat: Улучшена аутентификация и приватные маршруты
fix: Исправлены сообщения об ошибках аутентификации chore: Добавлена сортировка пользователей по ID
This commit is contained in:
parent
aadc4bf5bd
commit
67963bd395
@ -15,6 +15,7 @@ class UsersRepository:
|
||||
stmt = (
|
||||
select(User)
|
||||
.options(joinedload(User.role))
|
||||
.order_by(User.id)
|
||||
)
|
||||
result = await self.db.execute(stmt)
|
||||
return result.scalars().all()
|
||||
|
||||
@ -21,17 +21,20 @@ async def get_current_user(
|
||||
try:
|
||||
payload = jwt.decode(credentials.credentials, auth_data["secret_key"], algorithms=[auth_data["algorithm"]])
|
||||
except jwt.ExpiredSignatureError:
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Token has expired")
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Срок действия токена истек")
|
||||
except jwt.InvalidTokenError:
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token")
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Неправильный токен")
|
||||
|
||||
user_id = payload.get("user_id")
|
||||
if user_id is None:
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token")
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Неправильный токен")
|
||||
|
||||
user = await UsersRepository(db).get_by_id_with_role(user_id)
|
||||
if user is None:
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="User not found")
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Пользователь не найден")
|
||||
|
||||
if user.is_blocked:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Пользователь заблокирован")
|
||||
|
||||
return user
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user