Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 %}
like image 854
Maxtechwell Avatar asked May 18 '16 12:05

Maxtechwell


People also ask

Does Flask-login use session?

Configuring your ApplicationBy default, Flask-Login uses sessions for authentication. This means you must set the secret key on your application, otherwise Flask will give you an error message telling you to do so.

Which of the following can be used to login a user in Flask?

Flask-Login can manage user sessions. Start by adding the UserMixin to your User model. The UserMixin will add Flask-Login attributes to the model so that Flask-Login will be able to work with it. With Flask-Login setup, use the /login route.

What is UserMixin in Flask?

UserMixin class provides the implementation of this properties. Its the reason you can call for example is_authenticated to check if login credentials provide is correct or not instead of having to write a method to do that yourself.


1 Answers

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 %}
like image 197
syntonym Avatar answered Sep 19 '22 19:09

syntonym