Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use phone number to authenticate user in Django

Tags:

python

django

Is there a method for me to correctly use a user's phone number as a login/authenticate mechanism in Django? Till date I've used Django's in built auth system so I've never come across such a requirement.

However, now that I have, I can't seem to make any progress apart from figuring out that I'd have to use a One Time Password mechanism on sign up to authenticate.

like image 294
Newtt Avatar asked Jun 17 '15 08:06

Newtt


2 Answers

You can customize the User model and specify a different field be used as the username. This is mentioned in the auth documentation:

USERNAME_FIELD

A string describing the name of the field on the User model that is used as the unique identifier. This will usually be a username of some kind, but it can also be an email address, or any other unique identifier. The field must be unique (i.e., have unique=True set in its definition).

so something like:

class MyUser(AbstractBaseUser):
    phone_number = models.RegexField(...)
    ...
    USERNAME_FIELD = 'phone_number'

You should read through the authentication documentation, specifically "Customizing the User model" and "Substituting a custom User model"

like image 82
Timmy O'Mahony Avatar answered Oct 21 '22 04:10

Timmy O'Mahony


Just implement a custom authentication backend. Django has an example in the documentation using the email field. Just do the same but with a phone number field.

like image 26
argaen Avatar answered Oct 21 '22 03:10

argaen