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

Support OOP-style callbacks #333

Open
torotil opened this issue Jun 15, 2020 · 1 comment
Open

Support OOP-style callbacks #333

torotil opened this issue Jun 15, 2020 · 1 comment

Comments

@torotil
Copy link

torotil commented Jun 15, 2020

I’d love to see the possibly of simply sub-classing JWTManager and overwriting methods instead of defining callbacks using decorators. Currently it’s not possible to do that because the default callbacks are set in JWTManager.__init__() and thereby can only be overwritten after the manager has been instantiated.

Is there an advantage of registering the callbacks using decorators or is this just syntactic-sugar?

@vimalloc
Copy link
Owner

The reason we went with decorators instead of a more OOP approach was because that is the way most familiar for developers to interact with core flask and a bunch of existing flask add-ons, at least at the time this library was written. There is nothing wrong with an OOP way, it just isn't as common as decorators when dealing with flask code.

You could do an OOP approach do this right now, although it is a little funky because you have to dig into some private variables and use a static method for the callback:

    class CustomJWTManager(JWTManager):
        def __init__(self, app):
            super().__init__(app)
            self._expired_token_callback = self.handle_expired_token

        @staticmethod
        def handle_expired_token(jwt_header, jwt_data):
            return jsonify(foo="bar")

    app = Flask(__name__)
    app.config["JWT_SECRET_KEY"] = "foobarbaz"
    CustomJWTManager(app)

Maybe that is something that could be cleaned up in the 4.0.0 branch? If you want to take a stab at it I would welcome a PR!

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

No branches or pull requests

2 participants