Skip to content
Advertisement

How to tell if user’s email address has been verified using Django, allauth, rest-auth and a custom user

I’m using Django 2.0.10 with rest-framework, rest-auth and allauth. I have a custom user model.

I’ve got email verification working by using the allauth view. The verification email is sent when a user registers. If I click the link in the email, I’m taken to a page with a button to click to verify the email. This all works without error. However what I can’t find out is what this actually does. No field in the user’s data seems to change.

The behaviour I want is for users to be able to register and login, but only to be able to add content to the site after they have verified their email.

Edit: this post gives part of the answer but doesn’t say how to save the verification status as a property of the user so that you can check it in the front end when you load the user data.

settings.py

JavaScript

api/urls.py

JavaScript

users/models.py

JavaScript

When a user logs in, how can I find out if they have verified their email address? Thanks for any help!

Advertisement

Answer

Aha! Thanks to this post and this post, I think I have an answer.

The email address’s status is saved in a separate table EmailAdress, not as part of the User model. This can be accessed in a modelviewset as follows:

api.py

JavaScript

This will return True if the user has any verified email address.

However, it’s much more useful to add the verification status to the user. This can be done with a signal as explained here.

views.py

JavaScript

Now in api.py you can check like this:

JavaScript

This works if you have a single email address that can’t be changed or deleted. If you allow multiple email addresses I guess you’d need to make more checks and update the user’s status accordingly. But I have only a single email address which is used for login, so I think that’s OK.

I think it would be better practice to make ’email_verified’ part of a user profile, but this is a working demo.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement