Skip to content

Allow passing request/view context to JWT token generation#721

Open
AbhijithKonnayil wants to merge 4 commits into
iMerica:masterfrom
AbhijithKonnayil:720-No-way-to-pass-requestview-context-to-JWT-token-generation
Open

Allow passing request/view context to JWT token generation#721
AbhijithKonnayil wants to merge 4 commits into
iMerica:masterfrom
AbhijithKonnayil:720-No-way-to-pass-requestview-context-to-JWT-token-generation

Conversation

@AbhijithKonnayil
Copy link
Copy Markdown

@AbhijithKonnayil AbhijithKonnayil commented Dec 24, 2025

Fixes : #720

Currently, dj-rest-auth provides the user object to the JWT serializer, but there is no straightforward way to inject custom claims that depend on the request context (e.g., origin, device ID, user-agent) or view-specific logic.

This PR introduces a flexible mechanism to pass such context into the token generation process.

Changes

  • Serializers: Added TokenWithCustomClaimsSerializer which supports dynamic injection of extra claims.
  • Views: Added a hook get_non_user_custom_claims(request) in LoginView. Subclasses can override this to return a dictionary of claims derived from the request.
  • Utilities: Updated jwt_encode to accept and propagate custom_claims.
  • Settings: Updated default JWT_TOKEN_CLAIMS_SERIALIZER to the new custom claims serializer to support this feature out-of-the-box.
class MyLoginView(LoginView):
    def get_non_user_custom_claims(self, request, *args, **kwargs):
        return {
            'organization_id': request.data.get('org_id'),
            'ip_address': self.request.META.get('REMOTE_ADDR')
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No way to pass request/view context to JWT token generation

1 participant