'check_token in PUT method not working DRF
So I have a view that sends a confirmation email to the new user to activate with a uid and token. That part works fine. When the user clicks on the link it's suppose to send a PUT to my ActivateUserAPI where it grabs the 'uid' and 'token' from the url and sets email_confirmed = True, but it is not getting past djangos check_token function
url:
path('api/auth/activate/<path:uid>/<path:token>',
ActivateUserAPI.as_view(), name='activate'),
tokens.py
from django.contrib.auth.tokens import PasswordResetTokenGenerator
import six
class AccountActivationTokenGenerator(PasswordResetTokenGenerator):
def _make_hash_value(self, user, timestamp):
return (six.text_type(user.pk) + six.text_type(timestamp)) + six.text_type(user.email)
account_activation_token = AccountActivationTokenGenerator()
serializer:
class ActivationSerializer(serializers.ModelSerializer)
class Meta:
model = User
fields = ('id', 'email_confirmed',)
view:
class ActivateUserAPI(generics.UpdateAPIView):
serializer_class = ActivationSerializer
permission_classes = [
permissions.AllowAny
]
def put(self, request, *args, **kwargs):
token = self.kwargs['token']
try:
uid = force_str(urlsafe_base64_decode(self.kwargs['uid']))
user = User.objects.get(pk=uid)
print(account_activation_token.check_token(user, token)) # **this always returns False**
except(TypeError, ValueError, OverflowError, User.DoesNotExist):
user = None
if user is not None and account_activation_token.check_token(user, token):
serializer = ActivationSerializer(user, data=request.data)
if serializer.is_valid():
user.email_confirmed = True
user.save()
return Response(serializer.data)
return HttpResponse('Thank you for your email confirmation. Now you can login your account.')
else:
return HttpResponse('Activation link is invalid!')
The weird thing is that if I access the api directly through the url it works fine, like in the link below
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
