@@ -1,7 +1,10 @@ +from django.utils.translation import gettext as _ from ninja import Router from ninja.errors import HttpError +from rest_framework_simplejwt.exceptions import TokenError +from rest_framework_simplejwt.tokens import RefreshToken -from authentication.schemas import UserSchema +from authentication.schemas import UserSchema, RefreshInSchema, AccessOutSchema from authentication.security import SyncAuthBearer from authentication.selectors.user_selector import UserSelector @@ -15,3 +18,12 @@ def get_user_data(request): return UserSelector.detail(user=request.auth, provider=request.provider) except Exception as exc: raise HttpError(400, f'{exc}') + + +@router.post('refresh', auth=None, tags=['auth/refresh'], response=AccessOutSchema) +def refresh_token(request, payload: RefreshInSchema): + try: + refresh = RefreshToken(payload.refresh) + return AccessOutSchema(access=str(refresh.access_token)) + except TokenError: + raise HttpError(401, _('Invalid or expired refresh token')) @@ -43,3 +43,11 @@ class UserSchema(Schema): def resolve_account_type(obj: CustomUserModel): return obj.account_type + +class RefreshInSchema(Schema): + refresh: str + + +class AccessOutSchema(Schema): + access: str + @@ -67,7 +67,6 @@ urlpatterns = [ views.UpdateProfilePictureAPIView.as_view(), name='update-profile-pic', ), - path('token/refresh', TokenRefreshView.as_view(), name='refresh-jwt'), path( 'business-host', views.BusinessHostAPIView.as_view(), @@ -1373,6 +1373,10 @@ msgstr "Отсутствует обязательный параметр: 'messa msgid "Model not found" msgstr "Модель не найдена" +#: authentication/routes/v1.py:28 +msgid "Invalid or expired refresh token" +msgstr "Неверный или истёкший refresh токен" + #~ msgid "Regular users cannot send introductory letters" #~ msgstr "Обычные пользователи не могут отсылать письма"