[Solved]-How to check if a Django user is still logged in from the client side only?


The sessionid cookie exists and has an expiry date. You could check to see if that is still valid which would suggest they are logged it. You can’t get the username this way though, you’ll have to add a custom cookie.


Have code like the following in your template.

{% block top %}<script async="async" defer="defer" type="text/javascript" src="{% static 'users/js/geo.js' %}"

{% if user.is_authenticated %} data-authenticated="true" {% endif %}

>{% endblock %}

And check if your authenticated with code like:

unsafe_authenticated = 'true' === document.currentScript.dataset.authenticated;

Leave a comment