A simple session manager
This module is 100% specific and only works in combination with KissJS server.
Dependencies:
- kiss.ajax, to send credentials to the server
- kiss.views, to popup the login window
- kiss.router, to route to the right application view if session is valid
- kiss.websocket, to init the connection, to check that it's alive and reconnect if not
Members
# static maxIdleTime
Max idle time (30 minutes by default) After that delay, the user is logged out and its tokens are deleted from localStorage
Methods
# static addHook(event, callback)
Add a hook to perform an action before or after the session initialization
Parameters:
Name | Type | Description |
---|---|---|
event |
string
|
"beforeInit" | "afterInit" | "beforeRestore" | "afterRestore" |
callback |
function
|
Function to execute. It receives the following parameters: *beforeInit(sessionData), *afterInit(sessionData), *beforeRestore(), *afterRestore() |
this
Example
kiss.session.addHook("afterInit", function(sessionData) {
console.log("The session data is...", sessionData)
})
# async static checkTokenValidity(autoRenewopt) → {Promise.<boolean>}
Renew the current access token if needed. If token is not valid and can"t be renewed, return false
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
autoRenew |
boolean
|
<optional> |
true | If true, will try to renew the token if invalid token code (498) is received. |
Promise.<boolean>
# static getACL() → {Array.<string>}
Get the user's ACL.
Array containing all the user names and groups (32 hex id)
Array.<string>
# static getAccountId()
Get authenticated user's account id Offline and in-memory environments are anonymous
# static getCollaborators()
Get all current user's accounts he collaborates with
# static getCurrentAccountId()
Get authenticated user's current account id Offline and in-memory environments are anonymous
# static getExpiration()
Get token's expiration
# static getFirstName()
Get authenticated user's first name
# static getHttpHost() → {string}
Get the Http host with protocol and port
The host with protocol and port
string
# static getInvitations()
Get all users pending invitations to collaborate
# static getLastActivity()
Get the date/time of the last user activity which was tracked
# static getLastName()
Get authenticated user's last name
# async static getNewToken()
Gets a new token from the Refresh Token
The token, or false if it failed
# async static getServerEnvironment() → {string}
Get the application's server runtinme environment
"dev" | "production" | ... | "unknown"
string
# static getUserId()
Get authenticated user's id
# static getUserName()
Get authenticated user's full name Offline and in-memory environments are anonymous
# static getWebsocketHost() → {string}
Get the websocket host with protocol and port
The host with protocol and port
string
# static getWebsocketPort()
Get websocket non-secure port
# static getWebsocketSSLPort()
Get websocket secure port
# async static init(sessionData)
Set the session params:
- token
- expiration date
- accountId
- user's id
- user's first name
- user's last name
- user's account ownership
Parameters:
Name | Type | Description |
---|---|---|
sessionData |
object
|
# static initAccountManagers()
Initialize the account managers Note: a user is always an account manager for in-memory and offline mode
# static initAccountOwner()
Initialize the account owner Note: a user is always the account owner for in-memory and offline mode
# static isAccountManager()
Tell if the authenticated user is one of the account managers
# static isAccountOwner()
Tell if the authenticated user is the owner of the account
# static isAuthenticated()
Check if the user is authenticated
# static isIddle()
Check if the user is idle (= no mouse activity for n minutes)
# static isOffline()
Check if the environment is online/offline
# static logout()
Logout the user and redirect to the login page
# static reset()
Reset all kiss.session variables
# async static restore()
Restore session variables after a browser refresh
# static setDefaultViews(config)
Define the default views:
- login: view to login
- home: view to display after login
Parameters:
Name | Type | Description |
---|---|---|
config |
object
|
|
login |
string
|
Default = "authentication-login" |
home |
string
|
Default = "home-start" |
Example
kiss.session.setDefaultViews({
login: "your-login-view",
home: "your-home-view"
})
# static setHost(config)
Set the host for session requests. Host will be completed with protocol and port
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
config |
object
|
||
host |
string
|
<optional> |
|
httpPort |
number
|
<optional> |
|
httpsPort |
number
|
<optional> |
|
wsPort |
number
|
<optional> |
|
wssPort |
number
|
<optional> |
Example
kiss.session.setHost({
host: "your-host.com",
httpPort: 3000,
httpsPort: 4000,
wsPort: 3000,
wssPort: 4000
})
# static setLoginMethods(methods)
Set the possible login methods.
Possible login methods are currently:
- internal
- microsoftAD
- microsoft365
Parameters:
Name | Type | Description |
---|---|---|
methods |
Array.<string>
|
Example
kiss.session.setLoginMethods(["internal", "google"])
# static setMaxIdleTime(newIdleTime)
Set the maximum idle time before automatically logging out the user
Parameters:
Name | Type | Description |
---|---|---|
newIdleTime |
number
|
Max idle time in minutes |
# static setSecure(host)
Set the protocol security for session requests. If true (default):
- will use "https" for HTTP
- will use "wss" for Websocket
Parameters:
Name | Type | Description |
---|---|---|
host |
string
|
Example
kiss.session.setSecure(true)
# static showLogin(redirectoopt)
Show the login prompt
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
redirecto |
object
|
<optional> |
Route to execute after login, following kiss.router convention. Route to the home page by default. |
Example
kiss.session.showLogin({
ui: "form-view",
modelId: "0183b2a8-cfb4-70ec-9c14-75d215c5e635",
recordId: "0183b2a8-d08a-7067-b400-c110194da391"
})
# async static switchAccount(accountId) → {object}
Switch the user from one account to another
Parameters:
Name | Type | Description |
---|---|---|
accountId |
The /switchAccount response
object