django authenticate() still allways returns None

前端 未结 2 543
逝去的感伤
逝去的感伤 2021-01-29 06:17

I edited my question from yesterday: django authenticate() allways returns None, but I think noone will will pay attention because I already marked the question as answered. - a

相关标签:
2条回答
  • 2021-01-29 06:23

    On django version 2.1 or above, you should pass the request into function authenticate for the customized authentication. For example:

    class PasswordlessAuthenticationBackend(object):
    
        def authenticate(self, request, uid):
            try:
                token = Token.objects.get(uid=uid)
                print('got user', file=sys.stderr)
                return ListUser.objects.get(email=token.email)
            except ListUser.DoesNotExist:
                print('new user', file=sys.stderr)
                return ListUser.objects.create(email=token.email)
            except Token.DoesNotExist:
                return None
    

    Finally, don't forget the settings in settings.py: 1. accounts is your app name 2. ListUser is your model name

    AUTH_USER_MODEL = 'accounts.ListUser'
    AUTHENTICATION_BACKENDS = (
        'accounts.authentication.PasswordlessAuthenticationBackend',
    )
    
    
    0 讨论(0)
  • 2021-01-29 06:40

    Now authenticate() works. It was a version-mismatch. Now I remember that I created the initial project at home and on my gentoo-machine there still is the version 1.7.7 installed and on the windows-machine at work 1.8.4., I think it is the different settings-file, stupid mistake from me.

    Sorry for the inconvenience

    0 讨论(0)
提交回复
热议问题