check if user is logged in from UI script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2015 11:54 AM
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!
- Labels:
-
User Interface (UI)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2015 12:40 PM
Do you have access to the code of the page? You could check the user id server-side and wrap the relevant piece of Jelly code into <j:if> and </j:if> tags to hide it from certain users. Unauthenticated users must be using the out-of-box Guest account.
Blog: https://sys.properties | Telegram: https://t.me/sys_properties | LinkedIn: https://www.linkedin.com/in/slava-savitsky/

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2015 05:08 AM
I assume you have tried using g_user? Are you able to share what you tried? I did a bit of testing and g_user is available.
For anything like this where you need a standard piece of info that won't change in the client session it's much easier and better practice to just add a variable to the document on the server in the layout (or better, a UI macro that you include in each layout).
E.g. Content Management -> Design -> Layouts -> select your layout (you could also go via your CMS page layout field).
At the top of the XML, under the <j:jelly... tag, add this:
<script>
var u_is_logged_in = ${gs.isLoggedIn()};
</script>
Then in your script, you simply need to reference is_logged_in.
if (u_is_logged_in) {
// do some stuff.
}
It's also best, if possible, to hide things from your users by default and expose them if they are logged in. This way, no one sees stuff when the page is loading or if the script fails. Your specific use case will help determine this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2016 12:48 PM
Hi James,
I tried this. It didn't help me, it returns 'null' value. Please check.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2016 01:05 PM
Hey Kathy,
I was looking at similar issue. But some other post by Scott helped me. Please follow below link to check that.
Re: Getting a user object in a UI script