Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

login_user fails to get user id

My app fails whenever I call login_user with the error NotImplementedError: No 'id' attribute - override 'get_id'. My user has an id attribute. Why does this fail?

if form.validate_on_submit():
    user = User.query.filter_by(email=form.email.data).first()

    if user is not None and user.verify_password(form.password.data):
        print(user.user_id)
        login_user(user, False)
        return jsonify({'response': user.user_id})
class User(UserMixin, db.Model):
    __tablename__ = 'users'
    user_id = db.Column(db.Integer, primary_key = True)
    username = db.Column(db.String(64), unique=True, index=True)
    email = db.Column(db.String(64), unique=True, index=True)
    password_hash = db.Column(db.String(128))
like image 270
Brosef Avatar asked May 27 '16 00:05

Brosef


2 Answers

You just need to add a get_id() function in order to override the default properties of get_id() under the User class in the models.py file where your database schema is defined.

class User(#...):
    # ...
    def get_id(self):
           return (self.user_id)
    # ...
like image 110
Archana Prabhu Avatar answered Sep 20 '22 04:09

Archana Prabhu


login_user calls get_id on the user instance. UserMixin provides a get_id method that returns the id attribute or raises an exception. You did not define an id attribute, you named it (redundantly) user_id. Name your attribute id (preferably), or override get_id to return user_id.

like image 32
davidism Avatar answered Sep 22 '22 04:09

davidism