# Perspective - Log in / Log out event

**URL:** <https://forum.inductiveautomation.com/t/perspective-log-in-log-out-event/38168>\
**Category:** Ignition\
**Tags:** perspective, ignition80\
**Created:** [August 28, 2020, 3:24pm UTC](https://forum.inductiveautomation.com/t/perspective-log-in-log-out-event/38168 "2020-08-28T15:24:45Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![luc.martin](https://avatars.discourse-cdn.com/v4/letter/l/77aa72/32.png) [@luc.martin](https://forum.inductiveautomation.com/u/luc.martin)\
**Post date:** [August 28, 2020, 3:24pm UTC](https://forum.inductiveautomation.com/t/perspective-log-in-log-out-event/38168/1 "2020-08-28T15:24:46Z")

</div>

Is there a way to get events on log in and log out ?

I want to load / save session custom value when user log and log out

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [August 28, 2020, 3:40pm UTC](https://forum.inductiveautomation.com/t/perspective-log-in-log-out-event/38168/2 "2020-08-28T15:40:31Z")

</div>

I’d say the session.Startup and Shutdown events would fit the bill.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/2/023c20794901be7db272fb1213f94971a646edb3.png)

---

<div class="post-metadata">

**Author:** ![luc.martin](https://avatars.discourse-cdn.com/v4/letter/l/77aa72/32.png) [@luc.martin](https://forum.inductiveautomation.com/u/luc.martin)\
**Post date:** [August 28, 2020, 3:43pm UTC](https://forum.inductiveautomation.com/t/perspective-log-in-log-out-event/38168/3 "2020-08-28T15:43:25Z")

</div>

I want to save session value for each user, so I need to know the username, so session events not good for that.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 28, 2020, 4:07pm UTC](https://forum.inductiveautomation.com/t/perspective-log-in-log-out-event/38168/4 "2020-08-28T16:07:31Z")

</div>

Property change script on `session.props.auth.user.id`, or `session.props.auth.securityLevels`?

---

<div class="post-metadata">

**Author:** ![luc.martin](https://avatars.discourse-cdn.com/v4/letter/l/77aa72/32.png) [@luc.martin](https://forum.inductiveautomation.com/u/luc.martin)\
**Post date:** [August 28, 2020, 4:08pm UTC](https://forum.inductiveautomation.com/t/perspective-log-in-log-out-event/38168/5 "2020-08-28T16:08:55Z")

</div>

For sure, thanks

---

<div class="post-metadata">

**Author:** ![luc.martin](https://avatars.discourse-cdn.com/v4/letter/l/77aa72/32.png) [@luc.martin](https://forum.inductiveautomation.com/u/luc.martin)\
**Post date:** [August 28, 2020, 5:37pm UTC](https://forum.inductiveautomation.com/t/perspective-log-in-log-out-event/38168/6 "2020-08-28T17:37:14Z")

</div>

Have a trick to save the self.session.custom to a json field ?

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [August 31, 2020, 5:14am UTC](https://forum.inductiveautomation.com/t/perspective-log-in-log-out-event/38168/7 "2020-08-31T05:14:27Z")

</div>

What exactly are you trying to do?

---

<div class="post-metadata">

**Author:** ![luc.martin](https://avatars.discourse-cdn.com/v4/letter/l/77aa72/32.png) [@luc.martin](https://forum.inductiveautomation.com/u/luc.martin)\
**Post date:** [August 31, 2020, 11:26am UTC](https://forum.inductiveautomation.com/t/perspective-log-in-log-out-event/38168/8 "2020-08-31T11:26:51Z")

</div>

I wanted to build a json object to save it in a database.

Finally I have builded a script to do that.

```auto
def SignSession(previousUser, currentUser, session, cell_path):
	db_available = system.tag.read("[System]Gateway/Database/server_amt/Available")
	if db_available:
		if not previousUser is None:
			if not previousUser.value is None:	
				my_data = session.custom
				my_session = dict()
				for i in range(len(list(my_data))):
					my_session[list(my_data)[i]] = my_data[list(my_data)[i]]
				my_session["lang"] = session.props.locale
				result = _Functions.SaveSession(my_session, previousUser.value, session.custom.cell_path)
		
		if not currentUser is None:
			if not currentUser.value is None:
				my_session = _Functions.LoadSession(session.custom.cell_path, currentUser.value)
				if len(my_session):
					my_session = system.util.jsonDecode(my_session[0][0])
					if "lang" in my_session:
						session.props.locale = my_session["lang"]
					for key in my_session:
						if not key == "lang":
							session.custom[key] = my_session[key]
					return
		
		_Functions.DefaultSession(session, cell_path)	
		
def SaveSession(session, username, cell_path):
	import json

	try:
		my_timestamp = str(fctTimestamp(setTimestamp(),False))
		system.db.runPrepUpdate("INSERT INTO tbl_session_data (cell_path, username, session_data, last_session) VALUES ('" + str(cell_path) + "', '" + str(username) + "', '" + json.dumps(session) + "', '" + my_timestamp + "') ON CONFLICT (cell_path, username) DO UPDATE SET session_data=excluded.session_data, last_session=excluded.last_session RETURNING id;", getKey=1)
		return True
	except:
		return False	
	
def LoadSession(cell_path, username):
	session_data = system.db.runPrepQuery("SELECT session_data FROM tbl_session_data WHERE cell_path='" + str(cell_path) + "' AND username='" + str(username) + "'")
	return session_data

```

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [August 31, 2020, 12:00pm UTC](https://forum.inductiveautomation.com/t/perspective-log-in-log-out-event/38168/9 "2020-08-31T12:00:44Z")

</div>

You are not able to directly access the `session.custom` object; you would need to access `session.custom.someProperty`. I also don’t think `list(my_data)` will work because `my_data` would be an object, and as far as Jython is concerned, that’s equivalent to a dict already, so it looks like you’re obtaining a dict, then wrapping that dict in a list (so the list will only ever have one item), then iterating over that single-element list, and then attempting to assign the object as a dict key of the `my_session` object. Also, it looks like you’re passing in a `cell_path` variable but then you don’t use it because you only directly access a custom session property named `cell_path`, so watch out for that.

---

<div class="post-metadata">

**Author:** ![luc.martin](https://avatars.discourse-cdn.com/v4/letter/l/77aa72/32.png) [@luc.martin](https://forum.inductiveautomation.com/u/luc.martin)\
**Post date:** [August 31, 2020, 12:12pm UTC](https://forum.inductiveautomation.com/t/perspective-log-in-log-out-event/38168/10 "2020-08-31T12:12:53Z")

</div>

> [@cmallonee](#):
>
> You are not able to directly access the `session.custom` object; you would need to access `session.custom.someProperty` . I also don’t think `list(my_data)` will work because `my_data` would be an object, and as far as Jython is concerned, that’s equivalent to a dict already, so it looks like you’re obtaining a dict, then wrapping that dict in a list (so the list will only ever have one item), then iterating over that single-element list, and then attempting to assign the object as a dict key of the `my_session` object. Also, it looks like you’re passing in a `cell_path` variable but then you don’t use it because you only directly access a custom session property named `cell_path` , so watch out for that.

On my side all works well and did what I need.

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [August 31, 2020, 12:17pm UTC](https://forum.inductiveautomation.com/t/perspective-log-in-log-out-event/38168/11 "2020-08-31T12:17:29Z")

</div>

Ah, it looks like what I said is only true while binding - disregard.

---

<div class="post-metadata">

**Author:** ![luc.martin](https://avatars.discourse-cdn.com/v4/letter/l/77aa72/32.png) [@luc.martin](https://forum.inductiveautomation.com/u/luc.martin)\
**Post date:** [August 31, 2020, 8:01pm UTC](https://forum.inductiveautomation.com/t/perspective-log-in-log-out-event/38168/12 "2020-08-31T20:01:03Z")

</div>

I have implemant a script on userName change value as you suggest me. All works well except a bizzare thing, I have to logout twice to be logged out correctly. At logout my script run correctly but user seems to still logged, when I logout an other times the user is logged out correctly.

The problems seems to be when I do the db transaction. When I skip the “SaveSession” I can logout by clicking 1 times.

 ![2020-08-31_15h56_35](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/5/15815f9e86483ea1b35ea10d6e2412bac4dc28c6.gif)

Can you help me to fixed it?

My userName change event

```auto
_Session.SignSession(previousValue, currentValue, self, self.props.auth.authenticated)

```

My \_Session module

```auto
def DefaultSession(session, cell_path):
	session.custom["cell_path"] = cell_path
	session.custom["unit_area"] = "square_feet"
	session.custom["unit_distance"] = "mm"
	session.custom["unit_force"] = "newton"
	session.custom["unit_pressure"] = "kPa"
	session.custom["unit_speed"] = "mm_per_second"
	session.custom["unit_temperature"] = "celsius"
	session.custom["unit_times"] = "millisecond"
	session.props.locale = session.custom.locale_default 
	return

def SignSession(previousUser, currentUser, session, auth):
	db_name = system.dataset.toPyDataSet(system.db.getConnectionInfo())[0][0]
	db_available = system.tag.read("[System]Gateway/Database/" + db_name + "/Available")
	if db_available:
		if auth:
			my_session = LoadSession(session.custom.cell_path, currentUser.value)
			if len(my_session):
				my_session = system.util.jsonDecode(my_session[0][0])
				if "lang" in my_session:
					session.props.locale = my_session["lang"]
				for key in my_session:
					if not key == session.props.locale:
						session.custom[key] = my_session[key]
		
		if not previousUser is None:
			if not previousUser.value is None:	
				my_data = session.custom
				my_session = dict()
				for i in range(len(list(my_data))):
					my_session[list(my_data)[i]] = my_data[list(my_data)[i]]
				my_session["lang"] = session.props.locale
				SaveSession(my_session, previousUser.value, session.custom.cell_path)
				
		if not auth:
			DefaultSession(session, session.custom.cell_path)
	return
		
def SaveSession(my_session, username, cell_path):
	import json

	try:
		my_timestamp = str(_Functions.fctTimestamp(_Functions.setTimestamp(),False))
		system.db.runPrepUpdate("INSERT INTO tbl_session_data (cell_path, username, session_data, last_session) VALUES ('" + str(cell_path) + "', '" + str(username) + "', '" + json.dumps(my_session) + "', '" + my_timestamp + "') ON CONFLICT (cell_path, username) DO UPDATE SET session_data=excluded.session_data, last_session=excluded.last_session;")
		return True
	except:
		return False	
	
def LoadSession(cell_path, username):
	session_data = system.db.runPrepQuery("SELECT session_data FROM tbl_session_data WHERE cell_path='" + str(cell_path) + "' AND username='" + str(username) + "'")
	return session_data

```

---

<div class="post-metadata">

**Author:** ![BVTV](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@BVTV](https://forum.inductiveautomation.com/u/BVTV)\
**Post date:** [September 8, 2023, 1:38pm UTC](https://forum.inductiveautomation.com/t/perspective-log-in-log-out-event/38168/13 "2023-09-08T13:38:18Z")

</div>

> [@paul-griffith](#):
>
> Property change script on `session.props.auth.user.id`, or `session.props.auth.securityLevels`?

I am trying to attach a change script to the session.props.auth.user.id, but the script doesnt get saved. It disappears right after I press OK in the script edit window. Why does this happen?

I am able to add a change script to the property in the parent project, but not the one I am currently working in.  
(`8.1.27`)
