24

I have simple jinja2 template with registration/login links, I should hide them when user logged in, I also use flask_login module for this stuff.

Question is: How should I identify is user logged in in jinja2 templates?

Slow Harry
  • 1,777
  • 3
  • 21
  • 40

1 Answers1

54

Flask-Login adds the current_user variable to your templates:

{% if current_user.is_authenticated %}
    ...
{% else %}
    ...
{% endif %}

They mention this briefly in the documentation.

Chris Foster
  • 2,441
  • 1
  • 19
  • 28
Blender
  • 257,973
  • 46
  • 399
  • 459
  • 2
    [Flask-login docs](https://flask-login.readthedocs.org/en/latest/) do specify that `You can then access the logged-in user with the current_user proxy, which is available in every template` – Devi Jun 18 '15 at 09:13
  • 9
    Now is {% if current_user.is_authenticated %} – anvd Oct 01 '15 at 22:55
  • 1
    Note that when using `{{ current_user.is_authenticated }}` in javascript, that the value returned is not understood and must be put in a a string such as `"{{ current_user.is_authenticated }}"`. Came across this when validating onclick ajax requests for logged in users. – deesolie Oct 04 '20 at 04:24