django authenticate() still allways returns None

前端 未结 2 547
逝去的感伤
逝去的感伤 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',
    )
    
    

提交回复
热议问题