Skip to content

Commit

Permalink
auto update user.friendlyName and api auth header when user.name is u…
Browse files Browse the repository at this point in the history
…pdated
  • Loading branch information
cewert committed Jan 12, 2024
1 parent 8874b61 commit 3f172dc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
6 changes: 2 additions & 4 deletions source/ShowScenes.bs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ function LoginFlow()
startLoadingSpinner()
print "A public user was selected with username=" + userSelected
session.user.Update("name", userSelected)
regex = CreateObject("roRegex", "[^a-zA-Z0-9\ \-\_]", "")
session.user.Update("friendlyName", regex.ReplaceAll(userSelected, ""))

' save userid to session
for each user in publicUsersNodes
if user.name = userSelected
Expand Down Expand Up @@ -159,8 +158,7 @@ function LoginFlow()
print "Auth token found in registry"
session.user.Update("authToken", myAuthToken)
session.user.Update("name", myUsername)
regex = CreateObject("roRegex", "[^a-zA-Z0-9\ \-\_]", "")
session.user.Update("friendlyName", regex.ReplaceAll(myUsername, ""))

print "Attempting to use API with auth token"
currentUser = AboutMe()
if currentUser = invalid
Expand Down
44 changes: 30 additions & 14 deletions source/utils/session.bs
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,25 @@ namespace session
sub Update(key as string, value as dynamic)
' validate parameters
if key = "" or value = invalid then return

' make copy of global user session
tmpSessionUser = m.global.session.user
' update the temp user array
tmpSessionUser[key] = value

' keep friendlyName in sync
if key = "name"
regex = CreateObject("roRegex", "[^a-zA-Z0-9\ \-\_]", "")
tmpSessionUser["friendlyName"] = regex.ReplaceAll(value, "")
end if

' update global user session using the temp array
session.Update("user", tmpSessionUser)

' keep auth header in sync
if key = "name"
session.user.SetServerDeviceName()
end if
end sub

' Update the global session after user is authenticated.
Expand Down Expand Up @@ -187,28 +200,32 @@ namespace session
set_setting("active_user", tmpSession.user.id)
end if

' Save device id so we don't calculate it every time we need it
session.user.SetServerDeviceName()
' Load user preferences from server
session.user.LoadUserPreferences()
end sub

' Sets the global server device name value used by the API
sub SetServerDeviceName()
' default value is the unique id for the device
deviceName = m.global.device.id
if isValid(m.global.session.user) and isValid(m.global.session.user.friendlyName)
deviceName = deviceName + m.global.session.user.friendlyName
localGlobal = m.global

' default device name is the unique id for the device
deviceName = localGlobal.device.id
if isValid(localGlobal.session.user) and isValid(localGlobal.session.user.friendlyName)
deviceName = deviceName + localGlobal.session.user.friendlyName
end if

' update the global device array
tmpDevice = m.global.device
tmpDevice.AddReplace("serverDeviceName", deviceName)
m.global.setFields({ device: tmpDevice })
' update global if needed
if localGlobal.device.serverDeviceName <> deviceName
tmpDevice = localGlobal.device
tmpDevice.AddReplace("serverDeviceName", deviceName)
m.global.setFields({ device: tmpDevice })
end if
end sub

' Load and parse Display Settings from server
sub LoadUserPreferences()
' Save device id so we don't calculate it every time we need it
session.user.SetServerDeviceName()

id = m.global.session.user.id
' Currently using client "emby", which is what website uses so we get same Display prefs as web.
' May want to change to specific Roku display settings
Expand Down Expand Up @@ -377,6 +394,8 @@ namespace session

' load globals
session.user.settings.LoadGlobals()
' Reset server device name state
session.user.SetServerDeviceName()
end sub

' Grab global vars from registry and overwrite defaults
Expand All @@ -388,9 +407,6 @@ namespace session
session.user.settings.Save(item, get_setting(item))
end if
end for

' Reset server device name state
session.user.SetServerDeviceName()
end sub

' Saves the user setting to the global session.
Expand Down

0 comments on commit 3f172dc

Please sign in to comment.