Skip to content
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

custom token for other entity #757

Open
ternyavsky opened this issue Oct 16, 2023 · 1 comment
Open

custom token for other entity #757

ternyavsky opened this issue Oct 16, 2023 · 1 comment
Labels

Comments

@ternyavsky
Copy link

In my project, there are several entities that must be able to authorize. How can I write a custom token generation for each entity.
p.s. there was an option to use RefreshToken.for_user but it only uses the user model

@Andrew-Chen-Wang
Copy link
Member

You're definitely on the right track! I highly recommend overriding for_user such that you can decide which model to override. Unfortunately, I think this is a complex case that is out of scope for this library's implementation, but we're happy to guide!

I believe a good solution would be to extend the RefreshToken class' constructor such that it can accept a new value that you determine in the view/serializer. In the view/serializer, you can determine which entity to select, then pass that entity. For example (the below example may be slightly wrong as I answer a ton of issues, but it'll get you on the right track):

class RefreshToken(rest_framework_simplejwt):
    def __init__(self, *args, **kwargs):
        self.entity_to_use = kwargs.get("entity")

    def for_user(self, *args, **kwargs):
        # Override this portion to define your routing


# In settings.py
"refresh_token_class": "path.to.RefreshToken"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants