Check if user is logged in with Flask-Login in template

Maxtechwell picture Maxtechwell · May 18, 2016 · Viewed 10.3k times · Source

When I log a user in, I set logged_in in the session, then check this value in the template. Is there a better way to check if the user is logged in with Flask-Login?

session['logged_in'] = True
login_user(user)
{% if session['logged_in'] %}
    ...
{% endif %}

Answer

syntonym picture syntonym · May 18, 2016

Quoting the example on flask-login:

It’s that simple. You can then access the logged-in user with the current_user proxy, which is available in every template:

{% if current_user.is_authenticated %}
  Hi {{ current_user.name }}!
{% endif %}