Unable to swap after verifying login


I have code to scan a badge from a barcode reader and login the user.

But I want every window button to call to do this, and swap to the appropriate window after.

the code for loging in is below and works off of a user csv i import

# This script was generated automatically by the navigation
# script builder. You may modify this script, but if you do,
# you will not be able to use the navigation builder to update
# this script without overwriting your changes.

password = system.gui.passwordBox("Scan Badge")

system.nav.openWindow("Main Windows/TableImport")
window = system.gui.getWindow("Main Windows/TableImport")
data = window.rootContainer.getComponent("User Table").data
system.nav.closeWindow("Main Windows/TableImport")
user_name = ""

for row in range(data.rowCount):
	index_row = data.getValueAt(row, 3)
	if index_row == password:
		user_name = data.getValueAt(row, 1)

#Disables Read-Only-Mode, starts timer
system.tag.writeToTag("System1/Project1/ReadModeOnly", 0)
system.tag.writeToTag("System1/Project1/TimerState", 1)
system.nav.swapTo('Powder Main')
success = system.security.switchUser(user_name, password)

# If the login didn't work, give input focus back to the
# username component, so that the user can try again
if not success:
	system.tag.writeToTag("System1/Project1/ReadModeOnly", 1)
	system.tag.writeToTag("System1/Project1/TimerState", 0)
	window = system.gui.getWindow("Nav")
	window.rootContainer.getComponent("Timer").value = value

I got it to work by putting the swap to above the connect mode. But now I want it to redirect if the role should not have access