Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use flask_jwt_extended with blueprints?

Tags:

python

flask

jwt

I'm trying to build a blog as a portfolio sample using python3 and flask and flask_jwt_extended.

I can create a single file like this and it will run:

from flask_jwt_extended import (create_access_token, get_jwt_identity, JWTManager, jwt_required, get_raw_jwt)
from flask import Flask, request, Blueprint


app = Flask(__name__)

app.config['JWT_SECRET_KEY'] = 'this-is-super-secret'
app.config['JWT_BLACKLIST_ENABLED'] = True
app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['access']

jwt = JWTManager(app)


@app.route(....)
@jwt required

But when I try to use Blueprint, it will not register the JWTManager

Here is my user.py file:

from flask_jwt_extended import (create_access_token, get_jwt_identity, JWTManager, jwt_required, get_raw_jwt)
from flask import Flask, request, Blueprint


app = Flask(__name__)

app.config['JWT_SECRET_KEY'] = 'this-is-super-secret'
app.config['JWT_BLACKLIST_ENABLED'] = True
app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['access']

jwt = JWTManager(app)

user_blueprint = Blueprint('user_blueprint', __name__)

@user_blueprint.route(....)
@jwt required

here is my app.py:

from user import *
app = Flask(__name__)
app.register_blueprint(user_blueprint)

Now when I try to run app.py, this it returns a 500 (Internal Error) and will log this to the log file:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/flask_jwt_extended/utils.py", line 127, in _get_jwt_manager
    return current_app.extensions['flask-jwt-extended']
KeyError: 'flask-jwt-extended'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/ali/Desktop/cetia_api/user.py", line 63, in login
    return create_token(user_inputs)
  File "/home/ali/Desktop/cetia_api/user_functions.py", line 103, in create_token
    access_token = create_access_token(identity=data, expires_delta=expires)
  File "/usr/local/lib/python3.6/dist-packages/flask_jwt_extended/utils.py", line 156, in create_access_token
    jwt_manager = _get_jwt_manager()
  File "/usr/local/lib/python3.6/dist-packages/flask_jwt_extended/utils.py", line 129, in _get_jwt_manager
    raise RuntimeError("You must initialize a JWTManager with this flask "
RuntimeError: You must initialize a JWTManager with this flask application before using this method

Could someone please tell me what to do? I tried EVERYTHING for the last 3 days. its been 20 hours of debugging and it's still not fixed

like image 555
Rahimi0151 Avatar asked Aug 03 '19 06:08

Rahimi0151


1 Answers

You need to use the same app in every location. Currently you are creating an app in your users.py file, and a different app in you app.py file.

Generally you will want to use the flask application factory pattern to do this (https://flask.palletsprojects.com/en/1.1.x/patterns/appfactories/). An example might look something like this:

extensions.py

from flask_jwt_extended import JWTManager

jwt = JWTManager()

users.py

from flask_jwt_extended import (create_access_token, get_jwt_identity, jwt_required, get_raw_jwt)
from flask import Flask, request, Blueprint

user_blueprint = Blueprint('user_blueprint', __name__)

@user_blueprint.route(....)
@jwt_required

app.py

from extensions import jwt
from users import users_blueprint

def create_app():
    app = Flask(__name__)

    app.secret_key = 'ChangeMe!'
    app.config['JWT_BLACKLIST_ENABLED'] = True
    app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['access']

    jwt.init_app(app)

    app.register_blueprint(user_blueprint)

    return app

main.py

from app import create_app

app = create_app()

if __name__ == '__main__':
    app.run()
    
like image 169
vimalloc Avatar answered Sep 28 '22 07:09

vimalloc