How to check if a user is logged_in in a Drupal site via JavaScript?
Asked Answered
M

5

9

I know how to check if the user is logged in through PHP, but I need to do some styling when an event occurs, and I created a separate JavaScript file for this. Is this a Drupal variable or something which I can reference too?

Messeigneurs answered 16/4, 2012 at 22:35 Comment(1)
You're doing styling in javascript? Be very careful about what you do in javascript based on user authentication. It's VERY easy for anyone to manipulate the JS on your page.Postmistress
C
15

Create a new custom module with hook_init implementation.

function [YOUR_MODULE]_init()
{
    global $user;
    drupal_add_js(array('user_js_uid' => $user->uid), 'setting');
}

Then in your javascript code, check for the value of the variable defined in the module user_js_uid.

if(Drupal.settings.user_js_uid == 0)
{
    // execute code for non logged in users
}
else
{
    // execute code for logged in users
}
Chisel answered 17/4, 2012 at 6:46 Comment(5)
jQuery.cookie("DRUPAL_UID"); always returns nullHalicarnassus
There is no "DRUPAL_UID" cookie in 7.12Bicentenary
The second option is good, but note that it only applies if you're not behind Varnish or similar (which in our case is why we're doing something with JS in the first place).Broadcast
to get it from JS use uid = Drupal.settings.user_js_uid. Simply user_js_uid does not work for me in D7 - drupal.org/node/828916Aston
Too complicated to build a custom module. Haje's pure JavaScript method is good.Liebermann
B
14

If you are waiting for the DOM to be ready and use standard generated Drupal CSS classes, you could do something like (with jQuery):

if( $( "body.not-logged-in" ).length )
{
    // user is not logged in
}
Baer answered 30/5, 2013 at 6:42 Comment(2)
This is a great solution and you don't need to write a custom module if you're not using Varnish or similarMembranous
Like this one. This method no need to write PHP, just pure JavaScript.Liebermann
V
6

For Drupal 8 if you use Drupal.behaviors, you can access the user UID in settings:

(function($, Drupal, viewport, settings) {
  "use strict";
  Drupal.behaviors.ifCE = { //the name of our behavior
    attach: function (context, settings) {
    var userUID = settings.user.uid;
    //your code...
  }
})(jQuery, Drupal, ResponsiveBootstrapToolkit, drupalSettings);

So if the userUID === 0 then your user is not connected.

Vedda answered 13/3, 2017 at 8:25 Comment(0)
D
2

The "not-logged-in" class doesn't seem to exist in vanilla Drupal 8. But there's "user-logged-in" instead. Here's one line of CSS I'm using to hide the custom "Register" menu item if a user is logged in:

body.user-logged-in a[href="/user/register"] {display: none; }
Doodlesack answered 2/2, 2017 at 9:3 Comment(0)
D
0

I checked if the element #toolbar-administration exists since Drupal 10.

let isLoggedIn = jQuery("#toolbar-administration").length === 1;
Discount answered 23/8, 2023 at 14:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.