From e34c8ae3a71f63c94ec2ebfe963f2da9f4d5571e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B4=D1=80=D0=B5=D0=B9=20=D0=A1=D1=83=D0=BC?= =?UTF-8?q?=D0=B8=D0=BD?= Date: Sat, 1 Mar 2025 19:11:45 +0300 Subject: [PATCH] fix: incorrect token 500 --- services/backend/api/v1/auth.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/services/backend/api/v1/auth.py b/services/backend/api/v1/auth.py index bcc8870..75b571c 100644 --- a/services/backend/api/v1/auth.py +++ b/services/backend/api/v1/auth.py @@ -5,15 +5,19 @@ import jwt from django.conf import settings from django.http import HttpRequest from ninja.security import HttpBearer +from redis.exceptions import AuthorizationError from apps.user.models import User class BearerAuth(HttpBearer): def authenticate(self, request: HttpRequest, token: str) -> Any | None: - data = jwt.decode(token, settings.SECRET_KEY, algorithms=["HS256"]) - if data["exp"] < datetime.datetime.now().timestamp(): - return None + try: + data = jwt.decode(token, settings.SECRET_KEY, algorithms=["HS256"]) + if data["exp"] < datetime.datetime.now().timestamp(): + return None + except Exception: + raise AuthorizationError user = User.objects.get(id=data["id"]) return user