7

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.

Newtt
  • 6,050
  • 13
  • 68
  • 106
  • Try some thing like `auth using username` and put phone number there – itzMEonTV Jun 17 '15 at 08:39
  • Do you mean like send a text message to a phone number with a 1-time initial password? – Mike Covington Jun 17 '15 at 08:53
  • @mfcovington, yes. Django is being used in the backend for a phone application. So the client requires authentication by phone only. – Newtt Jun 17 '15 at 09:09
  • I'd start by checking out these links: http://stackoverflow.com/questions/430582/sending-an-sms-to-a-cellphone-using-django https://www.twilio.com/blog/2014/04/building-a-simple-sms-message-application-with-twilio-and-django-2.html https://www.djangopackages.com/grids/g/sms/ – Mike Covington Jun 17 '15 at 09:12
  • @mfcovington, What I want is that a user will be identified by a phone number. Not through anything else. Not exactly sending SMS, but actually identifying/logging in/registering with a phone number. – Newtt Jun 17 '15 at 09:18
  • Ah, then the answer below should work. – Mike Covington Jun 17 '15 at 09:20

2 Answers2

11

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"

Timmy O'Mahony
  • 53,000
  • 18
  • 155
  • 177
1

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.

argaen
  • 4,145
  • 24
  • 28