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
How to check if user is logged-in in javascript? Is there an function for Ajax that I can call? Thanks
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>
1-3 of 3