3

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.

1

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer Arpit Solanki.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.