Best way to auto-logout user if they sign into another session

Doing a search here appears there are a few suggestions on how to do the following:

If a user is logged into a session and then signs into another session, would like to automatically log out the old/previous session.

Is there a generally accepted ‘best’ practice for doing this?

For anyone searching this, I came up with my own solution that seems to work for all scenarios I have tried so far. Not sure if it is the most elegant or streamlined way, but it works well.

Create a client startup script (this runs anytime a new user logs in)

current_id = system.util.getClientId()
current_user = system.security.getUsername()

sessions = system.util.getSessionInfo()
match = False
for row in sessions:
	if row["username"] == current_user and row["clientId"] != current_id and row["isDesigner"] == False:
		match = True
		break
if match:
	system.util.sendMessage(project = project_name,
							messageHandler='AutoLogout',
							payload = {'message' : current_id}, 
							scope = 'C',
							user = current_user)
	system.gui.messageBox ("User's other open sessions have been logged out")						
						
if not match: (insert your other code if needed)

This will check the sessions. If the same username as the current user is in the list with a different client id that the one you are logging in with (and is not a designer session) then send a message to all other client sessions with the same username as the active user.

Then make a simple receive message script

system.gui.messageBox ("User logged into another session. This session will be logged out")
system.security.logout()

I thought I would have to do more code in the receive script to prevent the session sending the message from being logged out as well but it doesnt seem to work this way. I guess the session ‘sending’ the message also doesnt ‘receive’ that same message.

Open to any suggestions to improve upon this

3 Likes

It logout all login user’s not duplicate or specific .Can you please provide any solution for specific user auto logout ?

Hi may I know that anyone use the script above in perspective project. Currently I’m using the version of 8.1.3 but the script above dint work for me.

Try something like this, idk if its correct tho.
The current_id might be something else, i dont seem to get the same thing in the desinger, but it might work on client

sessions = system.perspective.getSessionInfo()
current_id = self.session.props.id
current_user = self.session.props.auth.user.userName

for row in sessions:
   if row["username"] == current_user and row["id"] != current_id and row["sessionScope"] != "designer":
      for page in row['pageIds']:
         system.perspective.logout(row["id"], page)
	

the “message box” will have to be a popup or somehting if you want that

Hi @victordcq it dint work for me as well.

I’m not sure whether the location I code is correct or not which I write my script in the session events startup. Can you help me to confirm whether I’m correct or not. Thanks in advance.

Did you try to do something like this before by the way thanks for your help.

you would have to run this code on the login i’d guess
or maybe page startup idk
i have not tried this before either

Okay Bro @victordcq I will try it later but when I try the code in a button onActionPerformed it prompt the error as the image show below.

Yaa I think I already put the code in the startup page. I will try it again later. Thanks Bro

ah ups right that should be a string
“designer” i edited it

Where you means bro ? Can you please help me to point it. Thanks Bro

at the end of the if

row["sessionScope"] != designer:
should be
row["sessionScope"] != "designer":

Okay Bro I will try it later I think the row[pageIds] should be a string also. Am I right bro ?

right that one need to be a string too between the brackets