问题
i am trying to implement django social auth in my project and i am having trouble integrating it with my custom user model. Whenever i try to login to facebook, it is throwing an error and always re-directing to LOGIN_ERROR_URL Page.
My settings.py file looks like below.
FACEBOOK_APP_ID = 'xxxxx'
FACEBOOK_API_SECRET = 'xxxxx'
AUTHENTICATION_BACKENDS = (
'social_auth.backends.facebook.FacebookBackend',
'django.contrib.auth.backends.ModelBackend',
)
SOCIAL_AUTH_PIPELINE = (
'social_auth.backends.pipeline.social.social_auth_user',
'social_auth.backends.pipeline.associate.associate_by_email',
'social_auth.backends.pipeline.misc.save_status_to_session',
'social_auth.backends.pipeline.user.create_user',
'social_auth.backends.pipeline.social.associate_user',
'social_auth.backends.pipeline.social.load_extra_data',
'social_auth.backends.pipeline.user.update_user_details',
'social_auth.backends.pipeline.misc.save_status_to_session',
)
LOGIN_URL = '/user/login_register/'
LOGIN_ERROR_URL = '/user/login_register/'
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'
SOCIAL_AUTH_COMPLETE_URL_NAME = 'socialauth_complete'
SOCIAL_AUTH_ASSOCIATE_URL_NAME = 'socialauth_associate_complete'
SOCIAL_AUTH_RAISE_EXCEPTIONS = False
SOCIAL_AUTH_FORCE_POST_DISCONNECT = True
SOCIAL_AUTH_USERNAME_IS_FULL_EMAIL = True
SOCIAL_AUTH_USER_MODEL = 'myapp.MyUser'
FACEBOOK_EXTENDED_PERMISSIONS = ['email']
AUTH_USER_MODEL = 'myapp.MyUser'
And my custom user model is defined as below (just uses e-mail and doesn't use firstname, lastname or username)
class MyUserManager(BaseUserManager):
def create_user(self, email, password=None):
"""
Creates and saves a User with the given email and password.
"""
if not email:
raise ValueError('Users must have an email address')
user = self.model(
email=MyUserManager.normalize_email(email),
)
user.set_password(password)
user.save(using=self._db)
return user
class MyUser(AbstractBaseUser):
email = models.EmailField(
verbose_name='Email address',
max_length=255,
unique=True,
db_index=True,
)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = MyUserManager()
USERNAME_FIELD = 'email'
Now when i try to login to my webapp using facebook login, it takes me to the facebook login credentials page and when i enter the login details, it throws me back to the LOGIN_ERROR_URL page. After i enter the facebook login details, it is not creating a new user in my app.
Can anyone help with what i am missing?
EDIT : -
The error i am getting is AuthFailed (Found user data for token) error message from below https://github.com/omab/django-social-auth/blob/master/social_auth/backends/facebook.py#L107
For more information, my login_resiter.html looks like below.
<h4> Or Login Using </h4>
<ul>
<li><a rel="nofollow" href="{% url "socialauth_begin" "facebook" %}">facebook</a></li>
<li><a rel="nofollow" href="{% url "socialauth_begin" "twitter" %}">twitter</a></li>
<li><a rel="nofollow" href="{% url "socialauth_begin" "google-oauth2" %}">google</a></li>
<li><a rel="nofollow" href="{% url "socialauth_begin" "yahoo" %}">yahoo</a></li>
</ul>
TEMPLATE_CONTEXT_PROCESSORS += (
'social_auth.context_processors.social_auth_by_name_backends',
'social_auth.context_processors.social_auth_login_redirect',
)
Thanks
来源:https://stackoverflow.com/questions/16253230/django-social-auth-with-custom-user-model-is-not-working