-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
45 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,40 @@ | ||
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer | ||
from rest_framework_simplejwt.serializers import ( | ||
TokenObtainPairSerializer, | ||
TokenRefreshSerializer | ||
) | ||
from rest_framework_simplejwt.settings import api_settings | ||
|
||
from datetime import timedelta | ||
from django.utils import timezone | ||
|
||
class CustomTokenObtainPairSerializer(TokenObtainPairSerializer): | ||
def validate(self, attrs): | ||
data = super().validate(attrs) | ||
|
||
now = timezone.now() | ||
|
||
access_token_lifetime = api_settings.ACCESS_TOKEN_LIFETIME | ||
|
||
data.update({ | ||
"id": self.user.id, | ||
'username': self.user.username, | ||
'email': self.user.email, | ||
"accessTokenExpires": int((now + access_token_lifetime).timestamp()), | ||
}) | ||
return data | ||
|
||
class CustomTokenRefreshSerializer(TokenRefreshSerializer): | ||
def validate(self, attrs): | ||
data = super().validate(attrs) | ||
|
||
access_token_lifetime = api_settings.ACCESS_TOKEN_LIFETIME | ||
refresh_token_lifetime = api_settings.REFRESH_TOKEN_LIFETIME | ||
|
||
now = timezone.now() | ||
|
||
data.update({ | ||
"access_expires_in": int((now + access_token_lifetime).timestamp()), # В секундах с 1970 | ||
"refresh_expires_in": int((now + refresh_token_lifetime).timestamp()), # В секундах с 1970 | ||
}) | ||
|
||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,21 @@ | ||
from django.urls import path, include | ||
|
||
from rest_framework_simplejwt.views import ( | ||
TokenObtainPairView, | ||
TokenRefreshView, | ||
) | ||
|
||
from rest_framework_simplejwt.views import TokenVerifyView | ||
|
||
from server.views.auth_view import CustomTokenObtainPairView | ||
from server.views.auth_view import ( | ||
CustomTokenObtainPairView, | ||
CustomTokenRefreshView | ||
) | ||
|
||
|
||
auth_urlpatterns = [ | ||
path('api/v1/drf-auth/', include('rest_framework.urls')), | ||
|
||
path('api/token/', CustomTokenObtainPairView.as_view(), name='token_obtain_pair'), | ||
path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'), | ||
path('api/token/refresh/', CustomTokenRefreshView.as_view(), name='token_refresh'), | ||
path('api/token/verify/', TokenVerifyView.as_view(), name='token_verify'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
from rest_framework_simplejwt.views import TokenObtainPairView | ||
from server.serializers.auth_serializers import CustomTokenObtainPairSerializer | ||
from rest_framework_simplejwt.views import ( | ||
TokenObtainPairView, | ||
TokenRefreshView | ||
) | ||
from server.serializers.auth_serializers import ( | ||
CustomTokenObtainPairSerializer, | ||
CustomTokenRefreshSerializer | ||
) | ||
|
||
class CustomTokenObtainPairView(TokenObtainPairView): | ||
serializer_class = CustomTokenObtainPairSerializer | ||
|
||
class CustomTokenRefreshView(TokenRefreshView): | ||
serializer_class = CustomTokenRefreshSerializer |