Allauth Users
Allauth Users
Install
poetry add django-allauth
Create accounts
app
python manage.py startapp accounts
Create a Custom User Model
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
from django.db import models
from django.utils import timezone
class CustomUserManager(BaseUserManager):
def create_user(self, email, password=None, **extra_fields):
if not email:
raise ValueError('The Email field must be set')
= self.normalize_email(email)
email = self.model(email=email, **extra_fields)
user
user.set_password(password)=self._db)
user.save(usingreturn user
def create_superuser(self, email, password=None, **extra_fields):
'is_staff', True)
extra_fields.setdefault('is_superuser', True)
extra_fields.setdefault(
return self.create_user(email, password, **extra_fields)
class CustomUser(AbstractBaseUser, PermissionsMixin):
= models.EmailField(unique=True)
email = models.CharField(max_length=30)
first_name = models.CharField(max_length=30)
last_name = models.BooleanField(default=True)
is_active = models.BooleanField(default=False)
is_staff = models.DateTimeField(default=timezone.now)
date_joined
= CustomUserManager()
objects
= 'email'
USERNAME_FIELD = ['first_name', 'last_name']
REQUIRED_FIELDS
def __str__(self):
return self.email
Update settings.py
in
myproject/settings.py
:
= [
INSTALLED_APPS # Django apps...
'django.contrib.sites', # Required by django-allauth
'allauth',
'allauth.account',
'allauth.socialaccount',
'accounts', # Your custom app
]
= 'accounts.CustomUser'
AUTH_USER_MODEL
= 1
SITE_ID
# Authentication backends
= (
AUTHENTICATION_BACKENDS 'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend',
)
= '/'
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL
# Optional: Email configuration for allauth
= 'django.core.mail.backends.console.EmailBackend'
EMAIL_BACKEND
# Custom allauth forms
= 'email'
ACCOUNT_AUTHENTICATION_METHOD = True
ACCOUNT_EMAIL_REQUIRED = 'mandatory'
ACCOUNT_EMAIL_VERIFICATION = False
ACCOUNT_USERNAME_REQUIRED = None
ACCOUNT_USER_MODEL_USERNAME_FIELD = 'accounts.forms.CustomSignupForm' ACCOUNT_SIGNUP_FORM_CLASS
Create a Custom Signup Form
In
accounts/forms.py
from django import forms
from allauth.account.forms import SignupForm
class CustomSignupForm(SignupForm):
= forms.CharField(max_length=30, label='First Name')
first_name = forms.CharField(max_length=30, label='Last Name')
last_name
def save(self, request):
= super(CustomSignupForm, self).save(request)
user = self.cleaned_data['first_name']
user.first_name = self.cleaned_data['last_name']
user.last_name
user.save()return user
Create Forms
In
accounts/forms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser
class CustomUserCreationForm(UserCreationForm):
class Meta:
= CustomUser
model = ('email', 'first_name', 'last_name', 'password1', 'password2')
fields
class CustomUserChangeForm(UserChangeForm):
class Meta:
= CustomUser
model = ('email', 'first_name', 'last_name', 'password', 'is_active', 'is_staff') fields
Register the Custom User Model in the Admin
In
accounts/admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import CustomUser
from .forms import CustomUserCreationForm, CustomUserChangeForm
class CustomUserAdmin(UserAdmin):
= CustomUserCreationForm
add_form = CustomUserChangeForm
form = CustomUser
model
= ('email', 'first_name', 'last_name', 'is_staff', 'is_active')
list_display = ('is_staff', 'is_active')
list_filter = ('email', 'first_name', 'last_name')
search_fields = ('email',)
ordering
= (
fieldsets None, {'fields': ('email', 'password')}),
('Personal Info', {'fields': ('first_name', 'last_name')}),
('Permissions', {'fields': ('is_staff', 'is_active', 'is_superuser')}),
('Important dates', {'fields': ('last_login', 'date_joined')}),
(
)= (
add_fieldsets None, {
('classes': ('wide',),
'fields': ('email', 'first_name', 'last_name', 'password1', 'password2', 'is_staff', 'is_active')}
),
)
admin.site.register(CustomUser, CustomUserAdmin)
Set Up URLs
myproject/urls.py
from django.contrib import admin
from django.urls import path, include
= [
urlpatterns 'admin/', admin.site.urls),
path('accounts/', include('allauth.urls')), # allauth URLs
path( ]
Run Migrations
May need to recreate the database