flask-jwt-extended
flask-jwt-extended copied to clipboard
Support OOP-style callbacks
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?
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!