pallets-eco / flask-jwt

JWT (JSON Web Tokens) for Flask applications
MIT License
564 stars 177 forks source link

Identity handler 'User' object is not callable #113

Closed carc1n0gen closed 7 years ago

carc1n0gen commented 7 years ago

I've set up a minimal flask app to test jwt. My authenticate handler works fine, but my identity handler keeps raising TypeError: 'User' object is not callable

app.py

from flask import Flask, current_app
from flask_sqlalchemy import SQLAlchemy
from flask_jwt import JWT, jwt_required, current_identity
from werkzeug.security import safe_str_cmp

app = Flask(__name__)
app.config.from_object('config')
DB = SQLAlchemy(app)
from app.jwt import authenticate, identity # Must import these after the database connection is made
jwt = JWT(app, authenticate, identity)

@app.route('/protected')
@jwt_required()
def protected():
    return current_identity

jwt.py

from werkzeug.security import safe_str_cmp
from bcrypt import checkpw
from app.database.models import User

def authenticate(username, password):
    user = User.query.filter(User.email == username).scalar()
    if user and checkpw(password.encode('utf-8'), user.password.encode('utf-8')):
        return user

def identity(payload):
    return User.query.filter(User.id == payload['identity']).scalar()
carc1n0gen commented 7 years ago

Hah never mind. It turns our I was trying to return the whole object from the flask view, which is a no go!