Custom users with django allauth

[亡魂溺海] 提交于 2019-12-11 00:04:26

问题


I am trying to use a custom user with django-allauth/social auth In settings.py, I have

AUTHENTICATION_BACKENDS = (
    # Needed to login by username in Django admin, regardless of `allauth`
    "django.contrib.auth.backends.ModelBackend",

    # `allauth` specific authentication methods, such as login by e-mail
    "allauth.account.auth_backends.AuthenticationBackend",
)

SOCIALACCOUNT_PROVIDERS = \
    {'facebook':
       {'SCOPE': ['email', 'user_likes', 'user_status', 'user_about_me', 'basic_info', 'read_stream'],
        'AUTH_PARAMS': {'auth_type': 'reauthenticate'},
        'METHOD': 'oauth2',
        'LOCALE_FUNC': 'path.to.callable',
        'VERIFIED_EMAIL': False}}


LOGIN_REDIRECT_URL = '/results/' 
AUTH_USER_MODEL = 'users.User'

In a folder users within the project folder, I have adapter.py:

from django.conf import settings
from allauth.account.adapter import DefaultAccountAdapter

class MyAccountAdapter(DefaultAccountAdapter):

def get_login_redirect_url(self, request):
    path = "/results/{username}/"
    return path.format(username=request.user.username)

In models.py:

from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
    user_data = models.TextField(null=True)

When I try to log in with facebook, I get redirected to facebook but returning to the site, I get the following error message

Django version 1.6.2
Exception Type: DoesNotExist
User matching query does not exist.

and in the console

"GET /results/facebook/login/? HTTP/1.1" 302 0
"GET /results/facebook/login/callback/XXX HTTP/1.1" 500

Any idea what I am doing wrong?


回答1:


I think you might be searching for a username with an email or viceversa. Try specifying this:

ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_AUTHENTICATION_METHOD = 'email'


来源:https://stackoverflow.com/questions/24066914/custom-users-with-django-allauth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!