Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login / register using phone or email for django, allauth integration

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:

  1. Can my user model be improved?
  2. Your thoughts on Allauth integration for phone & email authentication.
like image 214
Henry H Avatar asked Oct 29 '22 19:10

Henry H


1 Answers

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:

class User(AbstractBaseUser, PermissionsMixin):
    email = EmailLoginField(blank=True, unique=True, null=True) # used as login as can't be null.
    email2 = models.EmailField(blank=True, null=True)  # for users who joined with phone and added email to emailfield.
    phone = models.CharField(max_length=30, null=True, blank=True)

--- inside the phonenumber application

class PhoneNumber(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('user'), on_delete=models.CASCADE)
    phone = models.CharField(unique=app_settings.UNIQUE_PHONE, max_length=254, verbose_name=_('phone number'))
    verified = models.BooleanField(verbose_name=_('verified'), default=False)
    primary = models.BooleanField(verbose_name=_('primary'), default=False)

class PhoneConfirmation(models.Model):
    phone_number = models.ForeignKey(PhoneNumber, verbose_name=_('phone number'))
    created = models.DateTimeField(verbose_name=_('created'), default=timezone.now)
    sent = models.DateTimeField(verbose_name=_('sent'), null=True)
like image 69
Henry H Avatar answered Nov 09 '22 12:11

Henry H