Skip to content

Commit

Permalink
Add support for custom User subclasses (#171)
Browse files Browse the repository at this point in the history
## Description

This PR changes the behavior of the `DescopeUser` model somewhat.

Previously the `DescopeUser` model subclassed directly from Django's
built-in `User` model, and was configured as a proxy model. This works
fine if you only intend to use the Django built-in model, however this
does not work if a user wants to define a custom model via the
`AUTH_USER_MODEL` setting.

This PR makes use of the `get_user_model` function from Django, and
subclasses from that, which means that if a user specifies a custom
model from their application in the `AUTH_USER_MODEL` setting, then the
`DescopeUser` model will subclass from that.

## The two important things to note

`DescopeUser` is no longer a proxy model, this functionally I don't
think changes much, but does mean it creates it's own DB table, wether
it adds any new fields or not. This has been changed because in Django,
you can not create a proxy model off of another model which is not a
base model. So for example, the proxy model would work with Django's
built-in user as that is a base model, but it would not work on a custom
user model that subclasses from Django's built-in.

This does mean that the descope migrations can not be pre-shipped, and
must be generated during the user application's run of `makemigrations`,
because the DescopeUser migration will be dependent on the application's
User model(and this may be Django's built-in, but in this scenario
django-descope can't know that until the user creates their migrations).

---------

Co-authored-by: Omer Cohen <[email protected]>
  • Loading branch information
Cleptomania and omercnet authored Sep 7, 2023
1 parent b843bdc commit a931ece
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions django_descope/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import django.contrib.auth.models
from django.db import migrations
from django.conf import settings


class Migration(migrations.Migration):

initial = True

dependencies = [
Expand All @@ -21,7 +21,7 @@ class Migration(migrations.Migration):
"indexes": [],
"constraints": [],
},
bases=("auth.user",),
bases=(settings.AUTH_USER_MODEL,),
managers=[
("objects", django.contrib.auth.models.UserManager()),
],
Expand Down
4 changes: 2 additions & 2 deletions django_descope/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging

from descope import SESSION_TOKEN_NAME
from django.contrib.auth import models as auth_models
from django.contrib import auth
from django.core.cache import cache

from . import descope_client
Expand All @@ -10,7 +10,7 @@
logger = logging.getLogger(__name__)


class DescopeUser(auth_models.User):
class DescopeUser(auth.get_user_model()):
class Meta:
proxy = True

Expand Down

0 comments on commit a931ece

Please sign in to comment.