1

I was recycling an old app. I made a manager command to initialize the database with a few users. I then tried to log in, which went to the following method:

@user.route('/login', methods=['GET', 'POST'])
def login():
    """ log the user in """
    form = LoginForm()
    if form.validate_on_submit():
        u = User.query.filter_by(email=form.email.data.lower()).first()
        if u is not None and u.verify_password(form.password.data):
            login_user(u)
            flash('successfully logged in', 'success')
            return redirect(url_for('root.home'))
        flash('invalid login')
    return render_template('user/login.html', form=form)

The message flashed that the user had logged in successfully and no errors were thrown. However, none of the page changed to reflect that there was an authenticated user.

On my root blueprint (all things prefixed with / and nothing else), I added the following to aid in debugging

@root.before_app_request
def check_user():
    print('    is the user authenticated? {}\n    Who is the user? {}'.format(
        current_user.is_authenticated(),
        current_user
    ))

wben I made a few requests around,

 * Running on http://127.0.0.1:5000/
 * Restarting with reloader
    is the user authenticated? False
    Who is the user? <flask_login.AnonymousUserMixin object at 0x7f2454642550>
127.0.0.1 - - [04/Jul/2014 12:57:57] "GET / HTTP/1.1" 200 -
    is the user authenticated? False
    Who is the user? <flask_login.AnonymousUserMixin object at 0x7f245460eda0>
127.0.0.1 - - [04/Jul/2014 12:58:05] "GET /user/login HTTP/1.1" 200 -
    is the user authenticated? False
    Who is the user? <flask_login.AnonymousUserMixin object at 0x7f245460eda0>
127.0.0.1 - - [04/Jul/2014 12:58:10] "POST /user/login HTTP/1.1" 302 -
    is the user authenticated? False
    Who is the user? <flask_login.AnonymousUserMixin object at 0x7f24545cedd8>

what could be going wrong in this block of code that is not logging the user in? Full source code here: https://github.com/DarkCrowz/innovation_center

2
  • Your before_app_request could well be executed before Flask-login runs their before_app_request hook.. Commented Jul 4, 2014 at 17:06
  • I kinda figured that might be one of the things that happens, but after I navigate away from the POST to /user/login, the user is still not authenticated Commented Jul 4, 2014 at 17:24

1 Answer 1

2

You are using the id() function in your user loader:

@login_manager.user_loader
def load_user(identification):
    return User.query.get(id(identification))

identification is not a re-used memory location, yet your function treats it as if it is. Perhaps you meant to use int() here?

With id() replaced by int(), I get a valid session and logged in.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.