Newtt
Newtt

Reputation: 6190

Use phone number to authenticate user in 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.

Upvotes: 7

Views: 10025

Answers (2)

Timmy O'Mahony
Timmy O'Mahony

Reputation: 53971

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"

Upvotes: 11

argaen
argaen

Reputation: 4245

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.

Upvotes: 1

Related Questions