使用电话或电子邮件登录/注册Django,实现allauth集成 [英] Login / register using phone or email for django, allauth integration

查看:100
本文介绍了使用电话或电子邮件登录/注册Django,实现allauth集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想修改django用户模型以允许电话或电子邮件注册/登录.使用

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.)

这是我的account.models:

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)

我不太确定这是否是处理用户模型的正确方法,并且我更加困惑尝试将all-auth和rest-auth与之集成.现在,我的直觉告诉我只能自行创建所有内容.如果有人对全认证集成有任何经验,则可以进行电话/电子邮件注册和&登录,以及是否应该尝试分叉/只是从头开始,请让我知道.

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.

主要是我的两个问题:

  1. 我的用户模型可以改善吗?
  2. 您对电话和电话的Allauth集成的想法电子邮件身份验证.

推荐答案

我最终创建了一个用于存储和管理用户电话号码的新应用程序. 逻辑是,当用户使用电子邮件进行注册时,会触发常规的全身份验证流程.当用户使用电话号码注册时,我将使用自定义模型/视图/身份验证来登录用户,进行验证等.

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.

下面是我的模特:

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)

这篇关于使用电话或电子邮件登录/注册Django,实现allauth集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆