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

@Rosy14 

the line should not be in quotes

var CheckUserLoggedIn = Class.create();
CheckUserLoggedIn.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    u_user: function() {
        return gs.isLoggedIn();
    },
	type: 'CheckUserLoggedIn'
});

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

Yes.. I tried to sent a text msg. that also showing undefined

@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

Hi @Rosy14 ,

undefined because 

var x; at this point the x value is undefined

by the time getXMLAswer's Callback is called and newValue(response) to x 

the line after the getXMLAnswer will get executed so x value stays as undefined. once the system gets response from server callback is called and actual value is assigned 

 

try this or one that was added by @Ankur Bawiskar and check

 

(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;
                }
            }

        }
    });

})();

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

Robbie
Kilo Patron
Kilo Patron

Hi @Rosy14,

 

Within the Script Include, the syntax should include parenthesis as shown below. I also assume the script is client callable. 

gs.isLoggedIn()

 

Additionally, I would not use getXMLWait (unless absolutely necessary - It not best or preferred practice).

Instead use getXMLAnswer() and use a call back. For more info around this: 

https://www.servicenow.com/community/developer-articles/getxmlanswer-vs-getxml/ta-p/2307589

 

To help others (and for me to gain recognition for my efforts), please mark this response correct by clicking on Accept as Solution and/or Kudos.


Thanks, Robbie