Commit 94287f1b authored by BERJON Matthieu's avatar BERJON Matthieu
Browse files

Add authentication mechanism using allauth plugin

I added the local authentication mechanism using the allauth plugin.
Because the authentication through email doesn't work, I found a
workaround described here
https://stackoverflow.com/questions/49655402/email-authentification-doesnt-work-using-django-allauth



Now users can register and login using their email account. Registering
is achieved through email confirmation.
Signed-off-by: BERJON Matthieu's avatarMatthieu Berjon <matthieu.berjon@inria.fr>
parent fec7ec1b
from allauth.account.adapter import DefaultAccountAdapter
from django.core.validators import EmailValidator
class AccountAdapter(DefaultAccountAdapter):
"""Adapter for the default Django account using Allauth plugin
"""
def save_user(self, request, user, form, commit=True):
"""
Change the default behaviour to save the email as the username.
Compared to the original method, I don't save any data related to the
first and last name and force to save the email field as the username
instead of extracting the first part of the email (before the @) and
save it as the username.
This is a workaround for the issue of email signing issue.
"""
data = form.cleaned_data
user.email = data.get('email')
user.username = data.get('email')
if 'password1' in data:
user.set_password(data["password1"])
else:
user.set_unusable_password()
self.populate_username(request, user)
if commit:
# Ability not to commit makes it easier to derive from
# this adapter by adding
user.save()
return user
# Validator for the username as a proper email
custom_username_validators = [EmailValidator()]
......@@ -86,7 +86,6 @@ MIGRATION_MODULES = {
# ------------------------------------------------------------------------------
AUTHENTIFICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
# `allauth` specific authentication methods, such as login by e-mail
'allauth.account.auth_backends.AuthenticationBackend',
]
LOGIN_REDIRECT_URL = 'main:home'
......@@ -200,3 +199,15 @@ ADMINS = [
("""Matthieu Berjon""", 'matthieu.berjon@inria.fr'),
]
MANAGERS = ADMINS
# Allauth
# ------------------------------------------------------------------------------
ACCOUNT_AUTHENTICATION_METHOD = 'username_email'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
ACCOUNT_EMAIL_SUBJECT_PREFIX = '[A||go] '
ACCOUNT_CONFIRM_EMAIL_ON_GET = True
ACCOUNT_ADAPTER = 'allgo.main.adapter.AccountAdapter'
ACCOUNT_PRESERVE_USERNAME_CASING = False # force lowercase on username
ACCOUNT_USERNAME_VALIDATORS = 'allgo.main.adapter.custom_username_validators'
......@@ -10,7 +10,7 @@ urlpatterns = [
# Allgo stuff here
url(r'', include('main.urls')),
url(r'^users/', include('allauth.urls')),
url(r'^accounts/', include('allauth.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment