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 %}
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.
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.
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.
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 %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With