Ramkumar R
Ramkumar R

Reputation: 517

Custom model for authentication in django restframework instead of default user model

I want to do some custom auth for my users with username or email with password. At the first time of logging with email and password the api should return me the respective user token. For all other operations with the api I need to make use of token, which I get at time of login.

And I need a custom model to store all user info like username, password, phone, email, token etc.

How to achieve this in django restframework.

Please guide me to achieve this. Thanks in advance.

Upvotes: 3

Views: 835

Answers (1)

Arpit Solanki
Arpit Solanki

Reputation: 9931

Django rest-framework has a built in token system which can be used to distribute and authenticate tokens. Below is a sample of how to use it.

Create TOKEN

from rest_framework.authtoken.models import Token
user = User.objects.get(pk=some_pk)
token = Token.objects.create(user=user)

Authenticate token

if Token.ojects.get(key=token) # token is sent by client side 
    # do some task as auth is successful 

If you want to extend the default User model then create a new model and put a onetoone field in your custom model which references default User model.

class AppUserProfile(models.Model):
    user = models.OneToOneField(User)
    ... # add other custom fields like address or phone

Upvotes: 2

Related Questions