I've been trying to get Facebook login to work for my Django application. I have a custom user, except it seems that upon Facebook authentication, Facebook only passes back the user's username, whereas I would like the username, email, first_name, last_name, and profile picture. The other problem I have is that upon successful authentication, my app simply returns to the original URL with '#_=_'
.
My biggest issue is I don't know how to receive the proper results I need from the Facebook authentication and the documentation doesn't seem to cover it.
Settings.py
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'trending.backend.ActiviistUserBackend',
'social_auth.backends.facebook.FacebookBackend',
)
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'trending',
'gunicorn',
'cloudinary',
'social_auth',
)
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
SOCIAL_AUTH_FACEBOOK_PROFILE_EXTRA_PARAMS = {
'fields': 'id,name,email',
}
SOCIAL_AUTH_PIPELINE = (
'social_auth.backends.pipeline.social.social_auth_user',
'social_auth.backends.pipeline.associate.associate_by_email',
'social_auth.backends.pipeline.user.get_username',
'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.user.create_user'
)
LOGIN_URL = '/log_in/'
LOGIN_REDIRECT_URL = '/'
LOGIN_ERROR_URL = '/log_in/'
SOCIAL_AUTH_USER_MODEL = 'trending.ActiviistUser'
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
Log_in.html
<div id = 'wrapper'><div id = 'shiftlog'>
<h1>Log In</h1>
<br>
<form action="/log_in/" method="post">
{% csrf_token %}
{{ form.as_p}}
<input type="submit" value="Log In" />
</form><br><br>
<a href='/sign_up/' id = 'signup'>Need an Account?</a>
<a href="{%url 'socialauth_begin' 'facebook' %}">Login with Facebook</a>
</div></div>
models.py
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
# Create your models here.
class ActUserManager(BaseUserManager):
use_in_migrations = True
def create_user(self, email, **extra_fields):
"""
Creates and saves a User with the given username, email and password.
"""
print email
email = self.normalize_email(email)
user = self.model(username=email, **extra_fields)
user.save(using=self._db)
return user
class ActiviistUser (AbstractBaseUser):
firstname = models.CharField(verbose_name="First Name", max_length=30, default="")
lastname = models.CharField(verbose_name="Last Name", max_length=30, default="")
username = models.CharField(verbose_name="User Name", max_length=30, default="")
is_active = models.BooleanField(default=False)
email = models.EmailField(
verbose_name='Email Address',
max_length=255,
unique=True,
default=''
)
USERNAME_FIELD = 'email'
objects = ActUserManager()