How to check if logged in in UI script

Rosy14
Tera Guru

HI,

 

I want to check if the user is loggedin in portal. If not it will redirect to other url. In Ui script under Js inculde I added. For that I tried with gs.loggedIn and glideAjax, All are returing none.

var ga = new GlideAjax('sn_csm_uni_theme.CheckUserLoggedIn');
    ga.addParam('sysparm_name', 'u_user');
    ga.getXMLWait();
    var x = ga.getAnswer();
	
    if (window.location.href.toString().indexOf("sc_cat_item&sys_id") > 0) {

        var check = 0;
        for (var i = 0; i < ids.length; i++) {
            if (window.location.toString().indexOf(ids[i]) > 0) {
                check = 1;
                var url = window.location.href;
                url = url.replace('sc_cat_item', x);
                window.location.assign(url);
                break;
            }
        }
        // if (!gs.isLoggedIn) {
        //     if (check == 1) {
        //         var url = window.location.href;
        //         url = url.replace('sc_cat_item&sys_id', "sc_cat_item_public&sys_id");
        //         window.location.assign(url);
        //     }
        // }
    }
var CheckUserLoggedIn = Class.create();
CheckUserLoggedIn.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    u_user: function() {
        return gs.isLoggedIn;
    },
	type: 'CheckUserLoggedIn'
});
1 ACCEPTED SOLUTION

@Rosy14 

it means script include is not getting called

it seems your UI script is in scoped app and script include is in global?

share screenshots of both

try this and share the alerts

(function() {

    if (window.location.href.toString().indexOf("sc_cat_item&sys_id") > 0) {
		alert('Inside found');

        //    var ids1 = gs.getProperty("sn_csm_uni_theme.public.catalog.items.supplier").toString();
        //    var ids = ids1.split(",");
        var ga = new GlideAjax('sn_csm_uni_theme.CheckUserLoggedIn');
        ga.addParam('sysparm_name', 'u_user');
        var x;
        ga.getXMLAnswer(function(response) { // Use callback function
		alert('response came' + response);
            x = response;
            var ids1 = "260c306c1b120110735464a2b24bcbf9,c7d7070d1b9e0110c068fb25464bcb1c,f337587f1b56c110c068fb25464bcb6f,a5ed80991b164110c068fb25464bcbf8,2fd17fa41b6559500f52a797b04bcb8b,becc89cf1b5191500f52a797b04bcb42,7cc998f31b96c110c068fb25464bcbee";
            var ids = ids1.split(",");

            var check = 0;
            for (var i = 0; i < ids.length; i++) {
                if (window.location.toString().indexOf(ids[i]) > 0) {
                    check = 1;
                    var url = window.location.href;
                    url = url.replace('sc_cat_item', x);
                    window.location.assign(url);
                    break;
                }
            }
        });
    }
})();

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

10 REPLIES 10

Vishal Jaswal
Giga Sage

Hello @Rosy14 

Change:

u_user: function() { return gs.isLoggedIn; },

To:

u_user: function() { return gs.isLoggedIn(); }, // Call gs.LoggedIn as a function


Change:

ga.getXMLWait();
var x = ga.getAnswer();


To:

ga.getXMLAnswer(function(response) {  // Use callback function
   var x = response;


Change:

var ga = new GlideAjax('sn_csm_uni_theme.CheckUserLoggedIn');


To:

var ga = new GlideAjax('CheckUserLoggedIn'); // Removed scope prefix unless needed


If global then don't specify scope prefix.


Hope that helps!

Great minds @Vishal Jaswal - You beat me to it ; )

Thank you!


Hope that helps!

Still not working. returning undefined.

 

If I pass any text msg from script include to UI script that also returning undefined.

(function() {
    //    var ids1 = gs.getProperty("sn_csm_uni_theme.public.catalog.items.supplier").toString();
    //    var ids = ids1.split(",");
    var ids1 = "260c306c1b120110735464a2b24bcbf9,c7d7070d1b9e0110c068fb25464bcb1c,f337587f1b56c110c068fb25464bcb6f,a5ed80991b164110c068fb25464bcbf8,2fd17fa41b6559500f52a797b04bcb8b,becc89cf1b5191500f52a797b04bcb42,7cc998f31b96c110c068fb25464bcbee";
    var ids = ids1.split(",");

    var ga = new GlideAjax('sn_csm_uni_theme.CheckUserLoggedIn');
    ga.addParam('sysparm_name', 'u_user');
	var x;
    ga.getXMLAnswer(function(response) { // Use callback function
        x = response;
    });
    if (window.location.href.toString().indexOf("sc_cat_item&sys_id") > 0) {

        var check = 0;
        for (var i = 0; i < ids.length; i++) {
            if (window.location.toString().indexOf(ids[i]) > 0) {
                check = 1;
                var url = window.location.href;
                url = url.replace('sc_cat_item', x);
                window.location.assign(url);
                break;
            }
        }
        // if (!gs.isLoggedIn) {
        //     if (check == 1) {
        //         var url = window.location.href;
        //         url = url.replace('sc_cat_item&sys_id', "sc_cat_item_public&sys_id");
        //         window.location.assign(url);
        //     }
        // }
    }
})();
var CheckUserLoggedIn = Class.create();
CheckUserLoggedIn.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    u_user: function() {
        return "gs.isLoggedIn()";
    },
	type: 'CheckUserLoggedIn'
});