Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Button which log out and log in the current user automatically in Service Portal

Elena7
Tera Expert

Hello,

 

I created a button on the Service Portal.

When the user click on this button a message appear that he will be logged out then logged in via SSO.

 

I don't know how to do the logout/login part.

 

Does someone could help me on this ?

 

html 

<div>
  <button ng-if=data.result ng-click="c.uiAction('add')">
	Extend my rigths
  </button>
</div>

 

Client side

api.controller=function() {
	/* widget controller */
	var c = this;

	c.uiAction = function(action) {
		c.data.action = action;
		alert('You will be disconnected then reconnected');
		c.server.update().then(function() {
			c.data.action = undefined;
		})
	}
};

 

Server Side

(function() {
	/* populate the 'data' object */
	/* e.g., data.table = $sp.getValue('table'); */
	data.result=true;
	if (gs.hasRole('sn_incident_read') || gs.hasRole('incident_read_portal')){
		data.result=false;
	}
	//	gs.addInfoMessage(data.result);


	if (input && input.action) {
		var action = input.action;

		//If Add, check that user is not member and then add them

		if (action == 'add') {
			var me = gs.getUserID();
			var groupID = gs.getProperty('MY GROUP');

			var addUser = new GlideRecord('sys_user_grmember');
			addUser.addQuery('user',me);
			addUser.addQuery('group', groupID);
			addUser.query();
			if(!addUser.next())	{

				addUser.initialize();
				addUser.user = me;
				addUser.group = groupID;
				addUser.insert();

			}
		}
	}
})();

 

 

Thanks in advance,

Elena

2 REPLIES 2

Faizeal Mohamed
Tera Guru

Hi,

 

Please refere the OOB login widget for login

 

https://instance.service-now.com/sp_widget.do?sys_id=6506d341cb33020000f8d856634c9cdc

 

For logout 

use the below line in your Header widget HTML part

<li ng-if="::!(isViewNative || isViewNativeTablet)" class="visible-xs-block" role="presentation"><a role="menuitem" ng-href="{{::portal.logoutUrl}}" ng-click="collapse()">${Logout}</a></li>

 

Refer the OOB Widget Stock header:

 

https://instance_name.service-now.com/sp_header_footer.do?sys_id=bf5ec2f2cb10120000f8d856634c9c0c

 

Thanks,

Faizeal.

Elena7
Tera Expert

Hello,

 

Just to give an update. I found a solution.  

 On the server side I added:

var user = gs.getUser(); 
// Refresh GlideSession object
        GlideSecurityManager.get().setUser(user);                
// Refresh GlideUser object
         gs.getSession().loadUserByID(user.getID());

 

On the client side I added: window.location.reload();

 

Server script:

(function() {
	/* populate the 'data' object */
	/* e.g., data.table = $sp.getValue('table'); */
	data.result=true;
	if (gs.hasRole('sn_incident_read') || gs.hasRole('incident_read_portal')){
		data.result=false;
	}
	//gs.addInfoMessage(data.result);

	data.message =  gs.getMessage("In order to update your rights you will be disconnected and then reconnected");

	if (input && input.action) {
		var action = input.action;

		//If Add, check that user is not member and then add them
		if (action == 'add') {
			var me = gs.getUserID();
			var groupID = gs.getProperty('grp.role.incident.portail.auto');

			var addUser = new GlideRecord('sys_user_grmember');
			addUser.addQuery('user',me);
			addUser.addQuery('group', groupID);
			addUser.query();
			if(!addUser.next())	{

				addUser.initialize();
				addUser.user = me;
				addUser.group = groupID;
				addUser.insert();
				//gs.addInfoMessage('You have been added to the group.');
				
				var user = gs.getUser(); 
				
				// Refresh GlideSession object
        GlideSecurityManager.get().setUser(user);                
				
				// Refresh GlideUser object
         gs.getSession().loadUserByID(user.getID());

			}
		}
	}



})();

 

Client script:

api.controller = function() {
    /* widget controller */
    var c = this;
	alert(window.location.href);

    c.uiAction = function(action) {
        c.data.action = action;

        alert(c.data.message);
        c.server.update().then(function() {
            c.data.action = undefined;
						window.location.reload();
						//window.location.href = 'sun?id=sun_company_incidents';
            //window.location.href = '/logout.do';
					
        })

    }
};