TypeError: UserMixin.get_id() missing 1 required positional argument: 'self' - Flask Login

47 Views Asked by At

I'm new to flask login and trying to work out how it all works, Im sure its an easy fix but can't seem to see it anywhere.

I have built a USER model to start with and then added flask login after the event and I have the error TypeError: UserMixin.get_id() missing 1 required positional argument: 'self' when I try and log in.

So i have the following in my app.py

login_manager = LoginManager()
login_manager.init_app(app)
login_manager.session_protection = "strong"
login_manager.login_view = "login"
login_manager.login_message_category = "info"

from models.users import User


@login_manager.user_loader
def load_user(user_id):
    # since the user_id is just the primary key of our user table, use it in the query for the user
    return User.query.get(int(user_id))

and then ion my user model I have the following:

class User(UserMixin, db.Model):
    __tablename__ = "users"
    id = Column(Integer, primary_key=True)
    first_name = Column(String(16))
    last_name = Column(String(16))
    phone = Column(String(16))
    email = Column(String(32), unique=True)
    password = Column(String(128))

And the my login function looks like so

@app.route('/cms-login', methods=['GET', 'POST'])
def login():
    error = None
    if request.method == 'POST':
        email = request.form['username']
        password = request.form['password']
        test = User.query.filter_by(email=email).first()
        if test:
            if bcrypt.check_password_hash(test.password, password):
                login_user(User)
                return redirect(url_for('blogs_blueprint.home'))
            else:
                error = 'Password is incorrect. Please try again'
        else:
            error = 'Invalid Credentials. Please try again.'
    return render_template('login.html', error=error)
0

There are 0 best solutions below