# How to create a login on a Menu Tree?

**URL:** <https://forum.inductiveautomation.com/t/how-to-create-a-login-on-a-menu-tree/99905>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [December 16, 2024, 7:10pm UTC](https://forum.inductiveautomation.com/t/how-to-create-a-login-on-a-menu-tree/99905 "2024-12-16T19:10:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [December 16, 2024, 7:10pm UTC](https://forum.inductiveautomation.com/t/how-to-create-a-login-on-a-menu-tree/99905/1 "2024-12-16T19:10:45Z")

</div>

Hey guys,  
I have a project that does not require a login, because it is an anonymous survey, but for those that need to get the data I created this:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/5/f5e95115548d7f276cbef2899a8595d0f669bb25.png)

The "Reporting" item is hidden unless I am logged in, and the "Logout" is "Login" until authenticated. All I have so far is an event on the menu that sets a custom prop to true if the user is authenticated, and a script that runs every time the menu is clicked.

```python
def runAction(self, event):
	if self.custom.auth == True:
		system.perspective.logout()
	else: 
		system.perspective.login()

```

This does not work very well, because on each click event I am asked to login. So, what needs to happen to make this work?

Can I get the menu item that was clicked, such as the "Login/out" item? Which is index 2 in this case.

---

<div class="post-metadata">

**Author:** ![danieltalbot](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/danieltalbot/32/11201_2.png) [@danieltalbot](https://forum.inductiveautomation.com/u/danieltalbot)\
**Post date:** [December 16, 2024, 7:15pm UTC](https://forum.inductiveautomation.com/t/how-to-create-a-login-on-a-menu-tree/99905/2 "2024-12-16T19:15:35Z")

</div>

try this

`itemPath = [int(x) for x in list(event.itemPath)]`

You can compare this to the item path for your logout node and only run your script when they match

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 16, 2024, 7:17pm UTC](https://forum.inductiveautomation.com/t/how-to-create-a-login-on-a-menu-tree/99905/3 "2024-12-16T19:17:53Z")

</div>

I would suspect that your `auth` property isn't actually a boolean, and therefore **never** equal to true.

Consider using the session auth properties to drive your display and logic.

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [December 16, 2024, 7:19pm UTC](https://forum.inductiveautomation.com/t/how-to-create-a-login-on-a-menu-tree/99905/4 "2024-12-16T19:19:03Z")

</div>

I think I just found it. I can use `event.path` in the `onItemClicked` script.

No need for a loop.

It is boolean, the event script just needs some fixin'.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [December 16, 2024, 7:21pm UTC](https://forum.inductiveautomation.com/t/how-to-create-a-login-on-a-menu-tree/99905/5 "2024-12-16T19:21:18Z")

</div>

> [@mdemaris](#):
>
> It is boolean,

Then it's just  
`if self.custom.auth:`

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [December 16, 2024, 7:29pm UTC](https://forum.inductiveautomation.com/t/how-to-create-a-login-on-a-menu-tree/99905/6 "2024-12-16T19:29:42Z")

</div>

Oh, right, I forget the shorthand sometimes.

Thinking this might work:

```python
	if not self.custom.auth and event.path == 2:
		system.perspective.login()
	elif self.custom.auth and event.path == 2:
		system.perspective.logout()

```

Except, "Reporting" is not visible until logged in, therefore the `event.path = 2` will not work as `2` does not exist at that time...

So,

```python

	if not self.custom.auth and event.label == 'Login':
		system.perspective.login()
	elif self.custom.auth and event.label == 'Logout':
		system.perspective.logout()

```

This works. Is this an acceptable practice for allowing a login/out for something that would not normally require authentication?

---

<div class="post-metadata">

**Author:** ![Chris\_Bingham](https://avatars.discourse-cdn.com/v4/letter/c/e5b9ba/32.png) [@Chris\_Bingham](https://forum.inductiveautomation.com/u/Chris_Bingham)\
**Post date:** [December 16, 2024, 7:57pm UTC](https://forum.inductiveautomation.com/t/how-to-create-a-login-on-a-menu-tree/99905/7 "2024-12-16T19:57:58Z")

</div>

Edit: Was updating when new post came in... See below...

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [December 16, 2024, 8:00pm UTC](https://forum.inductiveautomation.com/t/how-to-create-a-login-on-a-menu-tree/99905/8 "2024-12-16T20:00:05Z")

</div>

This is more complex than it needs to be. You do not need `self.custom.auth` at all.  
Put a binding on your login item like so

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/f/1fc6cd0b8f7b0e694f3565188edc3df85d890865.png)  
Then in the `onItemClicked` event do this, and always check if the item is enabled, it is good practice.

```python
if event.enabled:
	if event.label == 'Login':
		system.perspective.login()
	elif event.label == 'Logout':
		system.perspective.logout()

```

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [December 16, 2024, 10:20pm UTC](https://forum.inductiveautomation.com/t/how-to-create-a-login-on-a-menu-tree/99905/9 "2024-12-16T22:20:40Z")

</div>

Yeah, I like it. Never used the map transform before. Thank you.
