4

I am really stuck in my project right now. I am trying to implement Oauth2 for my app. I found out about django-oauth2-provider a lot and tried it. The only problem is, it uses the User model at django.contrib.auth. The main users of our site are saved in a custom model called User which does not inherit from or extend the model at django.contrib.auth.

Is there any way to use my custom User model for creating clients and token?

If django-oauth2-provider can't be used for this purpose, can anyone recommend me some oauth2 library with the option to implement oauth2 with my own model.

Sincerely,

Sushant Karki

Gal Silberman
  • 3,756
  • 4
  • 31
  • 58
suzzant
  • 133
  • 1
  • 6
  • Is it possible to extend the default User model with your custom model? (not familiar with the django-oauth2-provider) But I think this will allow django-oauth2-provider to work normally. – Eagllus Aug 11 '14 at 13:03

3 Answers3

5

As the previous answer suggested, you should extend AbstractUser from django.contrib.auth.models.

The problem with the access token that the OP referring to, occur when changing the setting AUTH_USER_MODEL AFTER django-oauth2-provider was migrated.

When django-oauth2-provider is migrated, it creates a key constrain between the User model and django-oauth2-provider.

The solution is very easy:

  1. Create your new User model and change the AUTH_USER_MODEL setting.
  2. Go to the django_migration table in your database.
  3. Delete all rows of django-oauth2-provider.
  4. run python manage.py makemigrations
  5. run python manage.py migrate

Now, the django-oauth2-provider tables are connected to the RIGHT User model.

Gal Silberman
  • 3,756
  • 4
  • 31
  • 58
1

django-oauth2-provider fetches the user model using settings.AUTH_USER_MODEL, with a fallback to auth.User. If you extend AbstractUser your User model will include all the fields of auth.User plus any additional fields you specify.

from django.contrib.auth.models import AbstractUser
from django.db import models

class User(AbstractUser):
    some_additional_field = models.BooleanField(default=False)

Specify the user model to be used like this in settings.py:

AUTH_USER_MODEL = 'user_api.User'

If you don't want to base your user on AbstractUser you'll also need to write your own user manager, e.g. by extending the BaseUserManager

You can read more about ways to customize django's user model here.

Joar Leth
  • 691
  • 9
  • 27
  • 1
    I did it in the way you mentioned..But still i am getting issue with access token creation –  May 25 '15 at 11:50
0

bro, I faced the same problem I am clear about them exactly how to do a custom user model with Oauth2.

now I use SimpleJWT for the authentication

you can check this documentation it will help to solve your problem click here