Last active
November 1, 2024 07:53
-
-
Save AndrewPix/cdd9276b1d5683459b965d5cc4517b26 to your computer and use it in GitHub Desktop.
Integrate django-rest-knox with django-rest-auth
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
from rest_framework import serializers | |
from rest_auth.serializers import UserDetailsSerializer | |
class KnoxSerializer(serializers.Serializer): | |
""" | |
Serializer for Knox authentication. | |
""" | |
token = serializers.CharField() | |
user = UserDetailsSerializer() |
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
AUTHENTICATION_BACKENDS = ( | |
'django.contrib.auth.backends.ModelBackend', | |
'allauth.account.auth_backends.AuthenticationBackend', | |
) | |
REST_FRAMEWORK = { | |
'DEFAULT_AUTHENTICATION_CLASSES': ('knox.auth.TokenAuthentication',), | |
'DEFAULT_PERMISSION_CLASSES': ( | |
'rest_framework.permissions.IsAuthenticated', | |
), | |
'DEFAULT_FILTER_BACKENDS': ( | |
'django_filters.rest_framework.DjangoFilterBackend', | |
), | |
} | |
REST_AUTH_TOKEN_MODEL = 'knox.models.AuthToken' | |
REST_AUTH_TOKEN_CREATOR = 'project.apps.accounts.utils.create_knox_token' | |
REST_AUTH_SERIALIZERS = { | |
'USER_DETAILS_SERIALIZER': 'project.apps.accounts.serializers.UserDetailsSerializer', | |
'TOKEN_SERIALIZER': 'project.apps.accounts.serializers.KnoxSerializer', | |
} |
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
from knox.models import AuthToken | |
def create_knox_token(token_model, user, serializer): | |
token = AuthToken.objects.create(user=user) | |
return token |
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
from rest_framework.response import Response | |
from rest_auth.views import LoginView | |
from rest_auth.registration.views import RegisterView | |
from allauth.account.utils import complete_signup | |
from allauth.account import app_settings as allauth_settings | |
from .serializers import KnoxSerializer | |
from .utils import create_knox_token | |
class KnoxLoginView(LoginView): | |
def get_response(self): | |
serializer_class = self.get_response_serializer() | |
data = { | |
'user': self.user, | |
'token': self.token | |
} | |
serializer = serializer_class(instance=data, context={'request': self.request}) | |
return Response(serializer.data, status=200) | |
class KnoxRegisterView(RegisterView): | |
def get_response_data(self, user): | |
return KnoxSerializer({'user': user, 'token': self.token}).data | |
def perform_create(self, serializer): | |
user = serializer.save(self.request) | |
self.token = create_knox_token(None, user, None) | |
complete_signup(self.request._request, user, allauth_settings.EMAIL_VERIFICATION, None) | |
return user |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Main reason behind this is that,
When you are using dj_rest_auth registration method
In RegisterView there is method called get_response_data(self, user) which
return TokenSerializer(user.auth_token, context=self.get_serializer_context()).data
Here user.auth_token is main cause of errors raises
when you look on AuthToken in knox.models ,you found
user = models.ForeignKey(User, null=False, blank=False,
related_name='auth_token_set', on_delete=models.CASCADE)
whose related_name is auth_token_set not auth_token
Solution for this is,
1.Try to use custom register view as mention above
or change the auth_token to auth_token_set by overriding the
get_response_data(self, user) method of dj_rest_auth.registration.views.registerview
Hope you got it..