Home > Software engineering >  check_token() methon requesting for token argument yet it has been passed
check_token() methon requesting for token argument yet it has been passed

Time:07-09

I have been trying to create a reset password feature on my project I have been able to successfully send the reset password link to the email but am having trouble verifying the link sent. I am using the check_token method from PasswordResetTokenGenerator but I keep getting this error check_token() missing 1 required positional argument: 'token'. Yet I have passed both required argument

bellow is my Error and code:

Error

Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
    raise exc
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "C:\Users\User\Documents\vend\vendbackend-master\authentication\views.py", line 146, in get
    if not PasswordResetTokenGenerator.check_token(user,token):

Exception Type: TypeError at /api/authentication/password-reset/Mjk/b8a8se-b3e01964eb0d0f76a20c9418c9890a8b
Exception Value: check_token() missing 1 required positional argument: 'token'

views.py

class RequestPasswordReset(generics.GenericAPIView):
    serializer_class = RequestPasswordResetSerializer
    permission_classes = (permissions.AllowAny,)

    def post(self, request):
        data = {'request':request, 'data':request.data}
        serializer = self.serializer_class(data)
        email = request.data.get('email')

        if User.objects.filter(email=email).exists():
            user = User.objects.get(email=email)
            uidb64=urlsafe_base64_encode(smart_bytes(user.id))
            token = PasswordResetTokenGenerator().make_token(user)

            relativeLink= reverse('password-reset-confirm',kwargs={'uidb64':uidb64,'token':token})
            current_site = get_current_site(request).domain
            absurl='http://' current_site relativeLink
            email_body = 'Hello,\n  Use the link below to reset your password \n'  absurl
            data = {'email_body':email_body,'email_subject':"Reset your Password",'to_email':user.email}
    
            Util.send_email(data)
            return Response({'success':'We  have sent you a link to reset you password'},status=status.HTTP_200_OK)

        return Response({'error':'In  valid Email'}, status=status.HTTP_400_BAD_REQUEST)


class PasswordTokenCheckAPI(generics.GenericAPIView):
    permission_classes = (permissions.AllowAny,)
    
    def get(self,request,uidb64,token):
        
        try:
            uid = smart_bytes(urlsafe_base64_decode(uidb64))
            user = User.objects.get(id=uid)
            
            if not PasswordResetTokenGenerator.check_token(user,token):
                return Response({'error':'Token is invalid please request a new one'},status=status.HTTP_401_UNAUTHORIZED)
            
            return Response({'success':True,'message':'Credentials Valid','uidb64':uidb64,'token':token}, status=status.HTTP_200_OK)

        except DjangoUnicodeDecodeError as identifier:
            return Response({'error':'Token is invalid, please request a new one'},status=status.HTTP_401_UNAUTHORIZED)

url.py

urlpatterns = [
    ...
    path('password-reset/<uidb64>/<token>',PasswordTokenCheckAPI.as_view(), name='password-reset-confirm'),
    path('request-reset-email/',RequestPasswordReset.as_view(), name='request-reset-email'),
    ...
    
    
]

CodePudding user response:

try this

if not PasswordResetTokenGenerator().check_token(user,token):

CodePudding user response:

Im not a django expert, but it appears that it is eating one of your arguments, probably because it is expecting self as the first argument.

Try:

if not PasswordResetTokenGenerator().check_token(user,token):
    ...
  • Related