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
before_app_requestcould well be executed before Flask-login runs theirbefore_app_requesthook..