The CreatorCon Call for Content is officially open! Get started here.

check if user is logged in from UI script

kchisefsky
Mega Contributor

I have a CMS site where I need to hide html elements on various pages if the user is NOT logged in. I propose to do this in a global UI script but can't figure out how to check if the user is logged in; i.e., g_user will not work. Does anyone know or have an alternative?  

I know global UI scripts are not a preferred method but how else would you check client side for the page name, if user is logged in, and hide elements?

I have a cms page that is a menu page with a catalog request for field so I cannot specify a particular table in a client script.

Code examples and suggestion would be much appreciated.

Thank you!

8 REPLIES 8

ahaz86
Mega Guru

Here is what I had done previously to get a users id



UI Script


userCheck();


function userCheck(){


  var ga = new GlideAjax('CheckUser');


  ga.addParam('sysparm_name','u_user');


  ga.getXMLWait();


  alert(ga.getAnswer());


// HERE IS WHERE YOU WOULD WANT TO CHECK IF THE USER IS EMPTY


}



Script Include


Name: CheckUser


Client Callable: Checked


var CheckUser = Class.create();


CheckUser.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  u_user: function() {


  return gs.getUserID();


  }


});


Great idea but the userid comes back null or undefined even when I'm logged in. Did this ever happen to you? It looks like it should work.



Kathy Chisefsky


Web Developer


ActioNet, Inc.


20300 Century Boulevard, Suite 200


Germantown, MD 20874


Phone: 301-944-5681


Im sorry, in line 4 of the script include there is an error.



it should be


return gs.getUserID();




I have edited it in my above comment as well






Hey Alex,



I tried this script, but it returns 'null' value. Can you please check?



Thanks