pythonflaskflask-jwt

Customizing the Flask-JWT authenticate function


In the Flask initalization file, I have

app = Flask("myapp")
jwt = JWT(app, AuthController.staff_login, identity)
api.add_resource(StaffLogin, '/login')

StaffLogin checks for the username / password, and will in turn call AuthController.staff_login

class AuthController():
    def __init__(self):
        # TODO: Create a config.yaml
        self.engine = create_engine(DefaultConfig.SQLALCHEMY_DATABASE_URI)
        Base.metadata.create_all(self.engine)

        DBSession = sessionmaker(bind=self.engine)
        self.session = DBSession()                   

    def staff_login(self, staff_name, staff_password):
        result = self.session.query(Staff) # Irrelevant details of query removed
        if Staff_Authentication.check_password(staff_password, query['staff_password']):
                # Build login_response as JSON
                return login_response

Now I need a custom login response in this format

[
    {
        "projects": [
            {
                "project_id": 1,
                "project_name": "omnipresence",
                "project_root": "/path/to/project/files"
            }
        ],
        "staff_id": 13,
        "staff_name": "adalovelace"
    }
]

Question

How do I get Flask-JWT to use my function as authenticate?


Solution

  • Flask-JWT is old and has been abandoned. Check out flask-jwt-extended or flask-jwt-simple as alternatives, they are better designed and still maintained (I am the author of those extensions, so I am of course biased).

    They are setup so you provide your own endpoint instead of having the extension manage an endpoint for you.