pallets-eco / flask-jwt

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

AttributeError: 'dict' object has no attribute 'id' #115

Open sobolevn opened 7 years ago

sobolevn commented 7 years ago

When passing {'id': 'someId'} into the _default_jwt_payload_handler it raises an exception.

The problem is in this expression: identity = getattr(identity, 'id') or identity['id'] https://github.com/mattupstate/flask-jwt/blob/master/flask_jwt/__init__.py#L53

It must be: identity = getattr(identity, 'id', None) or identity['id']

daverbj commented 6 years ago

I have a user model looks like this

class UserModel(db.Model):
    __tablename__ = 'b_users'
    ID = db.Column(db.Integer, primary_key=True)
    user_login = db.Column(db.String(120), nullable=False)
    user_pass = db.Column(db.String(120), nullable=False)
    user_nicename = db.Column(db.String(120), nullable=False)
    user_email = db.Column(db.String(120), nullable=False)
    user_url = db.Column(db.String(120), nullable=False)
    user_registered = db.Column(db.String(120), nullable=False)
    user_activation_key = db.Column(db.String(120), nullable=False)
    user_status = db.Column(db.Integer, nullable=False)
    display_name = db.Column(db.String(120), nullable=False)

instead of id I have ID in the object so I get

identity = getattr(identity, 'id') or identity['id'] AttributeError: 'UserModel' object has no attribute 'id'

Is there any work around to use custom id field in identity object ?

sobolevn commented 6 years ago

I have just created a class instead of a dict to store my id as a property.

Kartstig commented 4 years ago

I'm also getting this error. I'm using namedtuple as a workaround

bartgenuit commented 3 years ago

I would also like to use another property name, since id is a Python built-in function.