Add user email verification to custom user model

105 views
Skip to first unread message

Manuel Buri

unread,
Mar 9, 2021, 9:47:37 AM3/9/21
to Django users
Hi, I am a bit lost while trying to add user email verification step to my custom user model that is based on AbstractBaseUser. Did someone do that already or if not how should I do it differently?
Thank you so much!

Gabriel Araya Garcia

unread,
Mar 9, 2021, 11:24:12 AM3/9/21
to django...@googlegroups.com
Your ask is not clear,..What is wrong ? What is the error message? If you need validate the email format, here is one javascript example :

function validaemail(email) {
if(email=="correo"){
    var x = document.getElementById("correo").value
} else {
    var x = document.getElementById("correo_apod").value
}

    if(x!='') {
        var expr = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if ( !expr.test(x) ) {
            alert("Error: La dirección de correo " + x + " es incorrecta.");
            document.getElementById("correo").value = "";
        }
    }
}

Regards,

Gabriel Araya Garcia
GMI - Desarrollo de Sistemas Informáticos
Santiago of Chile




El mar, 9 mar 2021 a las 11:47, Manuel Buri (<manue...@gmail.com>) escribió:
Hi, I am a bit lost while trying to add user email verification step to my custom user model that is based on AbstractBaseUser. Did someone do that already or if not how should I do it differently?
Thank you so much!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/aeec0130-5ef5-48f6-bb37-61d8ebeafd6fn%40googlegroups.com.

Manuel Buri

unread,
Mar 9, 2021, 11:38:52 AM3/9/21
to django...@googlegroups.com
Hi sorry for being unclear...
I currently have this standard user model:

from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager


class MyAccountManager(BaseUserManager):
def create_user(self, email, username, first_name, last_name, password=None):
if not email:
raise ValueError('Users must have an email address')
# if not username:
# raise ValueError('Users must have a username')
if not first_name:
raise ValueError('Users must have a first name')
if not last_name:
raise ValueError('Users must have a last name')

user = self.model(
email=self.normalize_email(email),
# username=username,
first_name=first_name,
last_name=last_name
)

user.set_password(password)
user.save(using=self._db)
return user

def create_superuser(self, email, password):
user = self.create_user(
email=self.normalize_email(email),
password=password,
# username=username,
)
user.is_admin = True
user.is_staff = True
user.is_superuser = True
user.save(using=self._db)
return user


class Account(AbstractBaseUser):
email = models.EmailField(verbose_name="email", max_length=60, unique=True)
# username = models.CharField(max_length=30, unique=True)
# TODO: for production do not allow null field
first_name = models.CharField(verbose_name="first_name", max_length=40)
last_name = models.CharField(verbose_name="last_name", max_length=40)
date_joined = models.DateTimeField(verbose_name='date joined', auto_now_add=True)
last_login = models.DateTimeField(verbose_name='last login', auto_now=True)
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)


USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name','last_name']

objects = MyAccountManager()

def __str__(self):
return self.email

# For checking permissions. to keep it simple all admin have ALL permissons
def has_perm(self, perm, obj=None):
return self.is_admin

# Does this user have permission to view this app? (ALWAYS YES FOR SIMPLICITY)
def has_module_perms(self, app_label):
return True
And know I would like to implement an email verification step that sets the user to active when verified using allauth django package.
But I do not know how to do it...

Best wishes,

Manuel





You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/YveSLX-SJRg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAKVvSDASwaPPf%3DK4kW7bDAV9yVke7Xh8E_Hr_3_A75gJYjSPnQ%40mail.gmail.com.

Kasper Laudrup

unread,
Mar 9, 2021, 11:48:51 AM3/9/21
to django...@googlegroups.com
On 09/03/2021 17.37, Manuel Buri wrote:
>
> And know I would like to implement an email verification step that sets
> the user to active when verified using allauth django package.
> But I do not know how to do it...
>

https://studygyaan.com/django/how-to-signup-user-and-send-confirmation-email-in-django

Kind regards,

Kasper Laudrup

Kasper Laudrup

unread,
Mar 9, 2021, 11:54:14 AM3/9/21
to django...@googlegroups.com
On 09/03/2021 17.22, Gabriel Araya Garcia wrote:
> Your ask is not clear,..What is wrong ? What is the error message? If
> you need validate the email format, here is one javascript example :
>

Actually, it is far from trivial or close to impossible to correctly
validate an email address using, eg. a regular expression:

https://emailregex.com/

The best you can do is to try to send an email to whatever is provided
and ask the user to confirm which seems to be what Manuel is trying to
do, although I agree that was far from clear in the original question.

Kind regards,

Kasper Laudrup

Manuel Buri

unread,
Mar 12, 2021, 10:22:37 AM3/12/21
to django...@googlegroups.com
Thank you very much for your help.
I made it work! Please let me know if I need to explain it to someone else.

Best wishes,

Manuel




--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/YveSLX-SJRg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d89dc3a4-8f17-0d32-bf40-c0db71885b5f%40stacktrace.dk.
Reply all
Reply to author
Forward
0 new messages