willer2k
willer2k

Reputation: 546

Forcing unique email address during registration with Django

Is there a simple way to force unique email address's during registration with website built with Django? I've seen some "addons?" like HMAC, but it seems a bit too complicated for what I am trying to achieve.

Also, would it be possible to accept registration only from a list of domains? (such as only emails from "@google.com")

Upvotes: 0

Views: 771

Answers (1)

Shivam Sharma
Shivam Sharma

Reputation: 919

I had the same problem and solved it by extending the AbstractUser class to my own class MyUser and changing the defaults.

Then by making the this class MyUser as a default model class for all my users I could apply this property(unique E-Mail) to all my users on my web app.

Create an app myuser. There in models.py:

from django.contrib.auth.models import AbstractUser
  
#create your own user class.
class MyUser(AbstractUser):
def __init__(self, *args, **kwargs):
    self._meta.get_field('email').blank = False
    self._meta.get_field('email')._unique = True
    super(MyUser, self).__init__(*args, **kwargs)

#Changed the defaults above.
#Give any additional field you want to associate your user with.

NOTE: AbstractUser already has all the basic fields you would want a User Model to have. For example: username, password, email etc. Check all of them here.

The last thing you would want to do is add the following in your setting.py

AUTH_USER_MODEL = 'myuser.MyUser'

This will make sure that the default user is associated with your web app is the extended(modified) MyUser class. This will provide you with all the basic functionalities that django provides for a User.

  1. login
  2. logout
  3. in your views: you can get user instance in: request.user etc.

I would like to suggest that you may need some additional code(in forms.py and views.py) to create a user through this type of class. I hope you will manage that. This should be enough to guide you in the right direction.

Maybe a library would have helped but since you needed an authentication for emails' domains as well, I think this should do the trick. In my humble opinion, you can't always depend on the 3rd party libraries for every other functionality.

Lastly, as you asked to authenticate a user coming only from a domain like @gmail.com or @outlook.com, a simple check in your django forms' clean method would do the trick. I hope you know how to handle django forms. If not, then you can learn about them in the official docs. They are an essential part of Django.

You can check the E-Mail with this logic:

email = self.cleaned_data['email']
email_source = email.split('@')[-1]
#email_source will now have values like: gmail.com, outlook.com etc
#you can now validate email_source now like:
permitted_sources = ['gmail.com' , 'outlook.com' , ]
if email_source in permitted_sources:
    return cleaned_data
else:
    raise forms.ValidationError('Error Message')
#Note: This logic should be kept in your clean method.

I hope this guides you. Thanks.

Upvotes: 2

Related Questions