I want to modify my django user model to allow phone or email registration / login. Using
USERNAME_FIELD = 'identifier'
If the user registers with phone number, the identifier will be its phone number, or email, vice versa. (If anyone think I should just assign some number as the identifier, let me know.)
Here is my accounts.models:
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager
from phonenumber_field.modelfields import PhoneNumberField
class UserManager(BaseUserManager):
def create_user(self, email, phone, password, **kwargs):
"""
Creates and saves a Account with the given email or phone and password.
"""
now = timezone.now()
identifier = ''
if not email:
if not phone:
raise ValueError('Users must have a valid email or phone address.')
else:
identifier = phone
if not phone:
if not email:
raise ValueError('Users must have a valid email or phone address.')
else:
email = self.normalize_email(email)
identifier = email
user = self.model(email=email, phone=phone,
identifier=identifier,
joined=now,
**kwargs
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, password, **kwargs):
user = self.model(
email=email,
is_staff=True,
is_superuser=True,
**kwargs
)
user.set_password(password)
user.save(using=self._db)
return user
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(null=True, blank=True, unique=True)
phone = PhoneNumberField(null=True, blank=True, unique=True)
joined = models.DateTimeField(auto_now_add=True)
first_name = models.CharField(max_length=200, null=True, blank=True)
last_name = models.CharField(max_length=200, null=True, blank=True)
is_staff = models.BooleanField(default=False)
objects = UserManager()
identifier = models.CharField(max_length=40, unique=True)
USERNAME_FIELD = 'identifier'
def get_username(self):
return self.email
def get_short_name(self):
return self.first_name
short_name = property(fget=get_short_name)
I'm not quite sure if this is the right way to approach user model, and I'm even more confused trying to integrate all-auth and rest-auth with it. Right now, my gut tells me to just create everything on my own. If anyone has any experience with all-auth integration so that it allows phone / email registration & login, and whether I should try to fork it / just start from scratch, please let me know.
So mainly these are my two questions:
- Can my user model be improved?
- Your thoughts on Allauth integration for phone & email authentication.
I ended up creating a new application for storing and managing phone numbers for users. Logic is that when the User signs up with an email, regular all-auth flow triggers. When the user signs up with a phone number, I use my custom models / views / authentication to log the user in, verify, etc.
Below are my models:
--- inside the phonenumber application