K. John
K. John

Reputation: 129

Getting type error while trying to authenticate user

I'm trying to authenticate users using a custom MyUser model in Django version 2.0.4. However, when the code hits the check_password line in my custom backend module, I get this error:

Error

Traceback:

    Traceback:
     File "D:\Python\lib\site-packages\django\core\handlers\exception.py" in inner
     35 response = get_response(request)

     File "D:\Python\lib\site-packages\django\core\handlers\base.py" in _get_response
     128 response = self.process_exception_by_middleware(e, request)

     File "D:\Python\lib\site-packages\django\core\handlers\base.py" in _get_response
     126 response = wrapped_callback(request, *callback_args, **callback_kwargs)

    File "d:\Programs\Python\Django\test2\accounts\views.py" in login_user
    52 user        = authenticate(request=request, email=email, password=password)

    File "D:\Python\lib\site-packages\django\contrib\auth\__init__.py" in authenticate
    70 user = _authenticate_with_backend(backend, backend_path, request, credentials)

    File "D:\Python\lib\site-packages\django\contrib\auth\__init__.py" in _authenticate_with_backend
    116 return backend.authenticate(*args, **credentials)

    File "d:\Programs\Python\Django\test2\accounts\backends.py" in authenticate
    29 if MyUser.check_password(password):

    Exception Type: TypeError at /accounts/login/
    Exception Value: check_password() missing 1 required positional argument: 'raw_password'

Here is my custom backend from backends.py:

    class EmailAuthenticationBackend(ModelBackend):
      def authenticate(self, request, email=None, password=None):
        MyUser = get_user_model()

        try:
          # Check the email/password and return a user
          user = MyUser.objects.get(email=email)

          # BUG
          if MyUser.check_password(password):
            return user

          except MyUser.DoesNotExist:
            return None

This is how the password come in. Login user view:

    def login_user(request):
      template_name   = 'registration/login.html'

      if request.method == 'POST':
        email       = request.POST['email']
        password    = request.POST['password']
        user        = authenticate(request=request, email=email, password=password)

While the error seems to be somewhat self explanatory, all the combos I've tried have failed. I've read the docs and searched other similar approach: nothing.

Upvotes: 0

Views: 1305

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599450

You should be calling that method on your instance, not the class.

if user.check_password(password):

Upvotes: 1

Related Questions