Skip to content

refactor: use AbstractBaseUser instead of AbstractUser for compatibility #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions ninja_jwt/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import django
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AbstractUser, AnonymousUser
from django.contrib.auth.models import AbstractBaseUser, AnonymousUser
from django.http import HttpRequest
from django.utils.translation import gettext_lazy as _
from ninja_extra.security import AsyncHttpBearer, HttpBearer
Expand Down Expand Up @@ -43,7 +43,7 @@ def get_validated_token(cls, raw_token) -> Type[Token]:
}
)

def get_user(self, validated_token) -> AbstractUser:
def get_user(self, validated_token) -> AbstractBaseUser:
"""
Attempts to find and return a user using the given validated token.
"""
Expand All @@ -64,7 +64,7 @@ def get_user(self, validated_token) -> AbstractUser:

return user

def jwt_authenticate(self, request: HttpRequest, token: str) -> AbstractUser:
def jwt_authenticate(self, request: HttpRequest, token: str) -> AbstractBaseUser:
request.user = AnonymousUser()
validated_token = self.get_validated_token(token)
user = self.get_user(validated_token)
Expand All @@ -86,7 +86,7 @@ class JWTStatelessUserAuthentication(JWTBaseAuthentication, HttpBearer):
def authenticate(self, request: HttpRequest, token: str) -> Any:
return self.jwt_authenticate(request, token)

def get_user(self, validated_token: Any) -> Type[AbstractUser]:
def get_user(self, validated_token: Any) -> Type[AbstractBaseUser]:
"""
Returns a stateless user object which is backed by the given validated
token.
Expand Down Expand Up @@ -119,7 +119,7 @@ def default_user_authentication_rule(user) -> bool:
class AsyncJWTBaseAuthentication(JWTBaseAuthentication):
async def async_jwt_authenticate(
self, request: HttpRequest, token: str
) -> Type[AbstractUser]:
) -> Type[AbstractBaseUser]:
request.user = AnonymousUser()
get_validated_token = sync_to_async(self.get_validated_token)
validated_token = await get_validated_token(token)
Expand Down