This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

kdoon80
kdoon80

How to check if user is logged-in in javascript? Is there an function for Ajax that I can call? Thanks

Flynsarmy
Flynsarmy

Option 1:

Load your login state on page load. Then just reference that - no actual AJAX requests required.

<!-- in your theme -->
{% if user %}
    var logged_in = true;
{% endif %}
<!-- In your JS -->
<script type="text/javascript">
    if ( logged_in ) ...
</script>

Option 2

This is an AJAX method.

// In your page/layout 'code' section
function onIsLoggedIn()
{
    return ['loggedIn' => !!Auth::check()];
}
<!-- In your JS -->
<script type="text/javascript">
$.request('onIsLoggedIn', {
    success: function(data){
      if ( data.loggedIn ) ...
    }
});
</script>
kdoon80
kdoon80

Thanks for the fast reply! Appreciate it :)

Last updated

1-3 of 3

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.