Options
All
  • Public
  • Public/Protected
  • All
Menu

Module User

JMap.User

From this section you can manage the user session.

Index

Functions

addInfo

  • JMap.User.addInfo

    Add a custom user info, stored in the redux store (JMap Core), and displayed in the user panel (JMap App).

    throws

    if info is not an object, if an attribute is missing or invalid type, if "id" already exists

    example
    
    // add a user information
    JMap.User.addInfo({
     id: "role",
     label: "Role",
     value: "Inspector"
    })

    Parameters

    Returns void

changeFullName

  • changeFullName(newFullName: string): Promise<void>
  • JMap.User.changeFullName

    For JMapCloud only.

    Changes the user's full name.

    throws

    Error if full name is not a string or is empty.

    example
    
    // change the user's full name
    JMap.User.changeFullName("John Doe")
      .then(() => console.info("Full name changed successfully"))
      .catch(error => console.error(error))

    Parameters

    • newFullName: string

      The user's new full name.

    Returns Promise<void>

changePassword

  • changePassword(newPassword: string, currentPassword: string): Promise<void>
  • JMap.User.changePassword

    Change the user password on JMap server

    throws

    Error if passwords are not strings or are empty, if new password is not valid or if newPassword's:

    • length is lower than the minimum length required (JMap Server)
    • strength is not high enough (JMap Cloud - at least 8 characters, at least one lowercase letter, one uppercase letter, one number, and one special character)
    example
    
    // change the password to warl3pE with a current password, grl83n5
    JMap.User.changePassword("warl3pE", "grl83n5")
    .then(() => console.info("Password changed successfully"))
    .catch(error => console.error(error))

    Parameters

    • newPassword: string

      The user new password

    • currentPassword: string

      The user current password

    Returns Promise<void>

getAllInfos

  • JMap.User.getAllInfos

    Returns all user informations. It is possible to add custom user information, available in the redux store and displayed in the user panel.

    This function returns all custom informations.

    example
    
    // returns all user infos
    JMap.User.getAllInfos()

    Returns JUserInfo[]

getFullName

  • getFullName(): string
  • JMap.User.getFullName

    Returns user full name.

    example
    
    // returns the user full name, ex : "John Do"
    JMap.User.getFullName()

    Returns string

getMinimumPasswordLength

  • getMinimumPasswordLength(): number
  • JMap.User.getMinimumPasswordLength

    Returns the minumum password length defined in JMap Core

    example
    
    // returns the minimum password length
    const MINIMUM_PASSWORD_LENGTH = JMap.User.getMinimumPasswordLength()

    Returns number

getOrganization

  • JMap.User.getOrganizationId

    Returns the currently logged in user's organization, if the server is a JMap Cloud server, otherwise an empty orgnization object.

    Organizations are only defined when connected to a JMap Cloud server instance.

    example
    
    // returns the user's organization
    JMap.User.getOrganization()

    Returns JOrganization

getOrganizationId

  • getOrganizationId(): string
  • JMap.User.getOrganizationId

    Returns user's organization id.

    If server is not a JMap Cloud server, or if the user in not logged in, will return an empty Id

    Organizations are only defined when connected to a JMap Cloud server instance.

    example
    
    // returns user's organization id
    JMap.User.getOrganizationId()

    Returns string

getPasswordPolicyCompliance

  • JMap.User.getPasswordPolicyCompliance

    Returns an object describing the password compliance with the platform's password policy (JMap Server or JMap Cloud)

    example
    
    // evaluate a password
    JMap.User.getPasswordPolicyCompliance("password")
    /*
       {
         "hasMinimumLength": true,
         "hasLowercaseLetters": true,
         "hasNumbers": false,
         "hasUppercaseLetters": false,
         "hasSpecialCharacters": false
       }
    /*

    Parameters

    • password: string

    Returns JJMapServerPasswordPolicyCompliance | JJMapCloudPasswordPolicyCompliance

getPreference

  • getPreference(name: string): Promise<string | null>
  • JMap.User.getPreference

    Get a user preference value from user storage. The returned Promise resolves to the value, else null if no preference has been set. Rejects on error, or if name parameter is not string or empty string, or if no user is logged in

    example
    
    let prefName = "jmap-core-basemap"
    JMap.User
     .getPreference(prefName)
     .then(preferenceValue => console.log(`Preference item "${prefName}" value is "${preferenceValue}"`))
     .catch(reason => console.log(`Cannot get the preference value of param "${prefName}". Reason: ${reason}`))
    

    Parameters

    • name: string

      the name of the preference

    Returns Promise<string | null>

    a Promise that resolves with the value from the user storage (or null if the preference is not set).

getToken

  • getToken(): string
  • JMap.User.getToken

    If user is logged in, returns the current user session token.

    Else returns "-1" if user has no active session.

    example
    
    // returns the user session token
    JMap.User.getToken()

    Returns string

getUsername

  • getUsername(): string
  • JMap.User.getUsername

    Returns the username (the one used to login).

    example
    
    // returns the username
    JMap.User.getUsername()

    Returns string

hasPreference

  • hasPreference(name: string): Promise<boolean>
  • JMap.User.hasPreference

    Check for a user preference existence. The returned Promise resolves with true if a value has been set for the user preference, else false. Rejects on error, or if name parameter is not string or empty string, or if no user is logged in

    example
    
    let prefName = "jmap-core-basemap"
    JMap.User
     .hasPreference(prefName)
     .then(hasPreferenceValue => console.log(`Preference item "${prefName}" exists: ${hasPreferenceValue.toString()}`))

    Parameters

    • name: string

      the name of the preference

    Returns Promise<boolean>

    a Promise that resolves with true if a value has been set for the user preference, else false

isLoggedIn

  • isLoggedIn(): boolean
  • JMap.User.isLoggedIn

    Returns true if a user is logged in.

    example
    
    // returns true if a user is logged in
    JMap.User.isLoggedIn()

    Returns boolean

isPasswordCompliant

  • isPasswordCompliant(password: string): boolean
  • JMap.User.isPasswordCompliant

    Returns true if the password complies with the platform's password policy (JMap Server or JMap Cloud), false otherwise.

    example
    
    // test a password
    JMap.User.isPasswordCompliant("password")
    // false

    Parameters

    • password: string

    Returns boolean

isPseudoUser

  • isPseudoUser(): boolean
  • JMap.User.isPseudoUser

    Returns true if the currently logged in user is a JMap pseudo user (ex: system, anonymous, etc...).

    example
    
    // returns true if current user is a pseudo user
    JMap.User.isPseudoUser()

    Returns boolean

isSystemUser

  • isSystemUser(): boolean
  • JMap.User.isSystemUser

    Returns true if the currently logged in user is a JMap system user (typically when NG is openned from JMap Admin).

    example
    
    // returns true if current user is a system user
    JMap.User.isSystemUser()

    Returns boolean

login

  • login(login: string, password: string): Promise<JSessionData>
  • JMap.User.login

    The login function, returns a promise. Make a call to the server and if login is successful resolve the promise providing the user session data.

    If an error occurs, 3 differents string message can be returned :

    • *"user.login.error.credential"* => Bad username or password
    • *"user.login.error.server"* => Unexpected error while requesting the server
    • *"user.login.error.unexpected"* => Unexpected error client side
    throws

    Error if bad credentials or server error.

    example
    
    const userLogin = "jdo@mycompany.com"
    const userPassword = "xxx"
    
    // Open a new user session, and get back user data from server
    JMap.User
       .login(userLogin, userPassword)
       .then(sessionData => {
         console.log(`User ${userLogin} has been authenticated, his session token is "${sessionData.accessToken}"`)
       })
       .catch(errorKey => {
         console.error(`Cannot loggin ${userLogin}, errorKey="${errorKey}"`, error)
       })

    Parameters

    • login: string
    • password: string

    Returns Promise<JSessionData>

loginIntoOrganization

  • loginIntoOrganization(organizationId: string): Promise<JSessionData>
  • JMap.User.loginIntoOrganization

    For JMapCloud only.

    Sets and returns Session Data specific to a JMap Cloud organization. You need to be previously authenticated via the JMap.User.login method before calling this method. This method can also be used to switch between organizations while a user is already logged in.

    throws

    Error if user is not authenticated

    example
    
    const userLogin = "jdo@mycompany.com"
    const userPassword = "xxx"
    
    // Open a new user session, and get back user data from server
    JMap.User
       .login(userLogin, userPassword)
       .then(sessionData => {
           console.log(`User ${userLogin} has been authenticated, will login to one of it's organization`)
           if(sessionData.organizationInfos.length === 0){
             console.error("User has no organization")
           }else{
             const organizationInfo = sessionData.organizationInfos[sessionData.organizationInfos.length - 1]
             JMap.User
               .loginIntoOrganization(organizationInfo.id)
               .then(sessionData2=>{
                 console.log(`User ${userLogin} has been logged into organization "${sessionData2.currentOrganization.name}", his session token is "${sessionData2.accessToken}"`)
               })
           }
       })
       .catch(error => {
         console.error(`Cannot loggin ${userLogin}, error: `, error)
       })

    Parameters

    • organizationId: string

    Returns Promise<JSessionData>

loginWithIdentityProvider

  • loginWithIdentityProvider(providerId: string): void
  • JMap.User.loginWithIdentityProvider

    Logs in the user using the specified Identity Provider. See JMap.Server.getAllIdentityProvidersById for info about Identity providers

    example
    
    // fetch all Identity Providers
    const allProviders = JMap.Server.getAllIdentityProvidersById()
    // { idp-1: { id: "idp-1", type: "sso", ..... } }
    
    // Open a new user session using the first provider received
    JMap.User.loginWithIdentityProvider("idp-1")
    // will redirect the browser to the identity provider for authentication

    @param providerId the JServerIdentityProvider id

    Parameters

    • providerId: string

    Returns void

logout

  • logout(): Promise<void>
  • JMap.User.logout

    Logout function. Make a call to the server to invalidate the session id.

    If an error occurs, 2 differents string message can be returned :

    • *"user.logout.error.server"* => Unexpected error while requesting the server
    • *"user.logout.error.unexpected"* => Unexpected error client side
    example
    
    // Close the user session client and server side
    JMap.User
       .logout()
       .then(() => {
         console.log(`User has been logout`)
       })

    Returns Promise<void>

removeInfo

  • removeInfo(infoId: string): void
  • JMap.User.removeInfo

    Remove a user info, from the redux store (JMap Core) and in the user panel (JMap App).

    example
    
    // remove the user information "role"
    JMap.User.removeInfo("role")

    Parameters

    • infoId: string

      The user info id

    Returns void

removePreference

  • removePreference(name: string): Promise<string | null>
  • JMap.User.removePreference

    Remove a user preference from user storage. The returned Promise resolves with the value of the removed preference, or null if the preference does not exist.

    Rejects on error, or if name parameter is not string or empty string, or if no user is logged in

    Parameters

    • name: string

      the name of the preference

      let prefName = "jmap-core-basemap" JMap.User .removePreference(prefName) .then(removedPreferenceValue => { if (removedPreferenceValue === null) { console.log(Preference item "${prefName}" did not exist or was not removed) } else { console.log(Preference item "${prefName}" has been removed. Value was: ${removedPreferenceValue}) } })

    Returns Promise<string | null>

    a Promise that removes the user preference, and resolves with the value of the removed preference, or null if the preference does not exist

setPreference

  • setPreference(name: string, value: string | undefined): Promise<void>
  • JMap.User.setPreference

    Set a user preference in user storage. The returned Promise resolves without value on success. Rejects with a reason on error, or if name parameter is not string or empty string, or if no user is logged in If passed value is undefined, the preference is removed

    example
    
    let prefName = "jmap-core-basemap"
    
    // Set the value "light" for user preference "jmap-core-basemap"
    JMap.User
     .setPreference(prefName, "light")
     .then(preferenceValue => console.log(`Preference item "${prefName}" has been set`))
     .catch(reason => console.log(`Cannot set the preference value of param "${prefName}". Reason: ${reason}`))
    
    // Remove the value for user preference "theme"
    JMap.User
     .removePreference(prefName)
     .then(preferenceValue => console.log(`Preference item "${prefName}" has been removed, and its value were "${preferenceValue}""`))
     .catch(reason => console.log(`Cannot remove the preference "${prefName}". Reason: ${reason}`))

    Parameters

    • name: string

      the name of the preference

    • value: string | undefined

      the value that will be associated to the name

    Returns Promise<void>

    a Promise that sets the user preference, and resolves with no value, or rejects with a reason

setToken

  • setToken(token: string, organizationId?: undefined | string): Promise<JSessionData>
  • JMap.User.setToken

    Sets the user session data. Useful if you want to make a call to our Rest API and set the session token by yourself.

    This process is a bit different for JMap Server than for JMap CLoud.

    For JMap Server, you need to fetch a session token from the REST API, and call JMap.User.setToken without spedifying the organization Id.

    For JMap Cloud, you need to fetch a refresh token from the JMap Cloud Rest API, and pass this refresh token, along with the the optional organisation Id, to the JMap.User.setToken method. Beware that a refresh token can only be used once, it is invalidated afterward

    Fetching data from a REST API can be done with the curl command-line tool (https://curl.haxx.se/docs/)

    a JMap Server example:

    # getting a session token from JMap Server
    curl -X POST "https://my-jmap-server/services/rest/v2.0/session" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"username\": \"jdo@company.com\", \"password\": \"xxx\", \"type\": \"NG\"}"

    will return something like:

    {
      "message": "The result is a NG session info",
      "status": "OK",
      "result": {
        ...
        "sessionId": 23558109, // session id in the Rest API response is the session token.
        ...
      }
    }

    a JMap Cloud example:

    # getting a session token from JMap Cloud
    curl --request POST \
        --url https://api.jmapcloud.io/api/ss/rest/v1/authenticate \
        --header 'accept: application/json' \
        --header 'content-type: application/json' \
        --data '
     {
        "username": "jdo@company.com",
        "password": "xxx"
     }
    '

    will return something like:

    {
     "message": "The result is the access and refresh tokens",
     "result": {
       "accessToken": "eyJhbGciOiJ [.....] 6qwoKzNXMML4oGyNP6Vw_fCC58LCb7YQnY431BaTmxMNswr0HKMN0PQ",
       "refreshToken": "v1.MRq [.....] Rehef72YWws",
       "accessTokenExpireAt": "2022-12-24T17:31:33.429+00:00",
       "accessTokenExpiration": 86400
     }
    }
    example
    
    // Set the user session token for JMap server
    JMap.User.setToken("23558109")
     .then(userData => {
       console.log(`Session token = "${userData.accessToken}""`)
       console.log(`The session belongs to ${userData.user.fullName}`)
     })
     .catch(error => {
       if (error === "user.token.invalid") {
         console.log(`Invalid token`)
       } else {
         console.log(`Server error`)
       }
     })
    
    // Set the user session token for JMap Cloud
    JMap.User.setToken("v1.MRq [.....] Rehef72YWws","my-organization-id")
     .then(userData => {
       console.log(`Session token = "${userData.accessToken}""`)
       console.log(`The session belongs to ${userData.user.fullName}`)
     })
     .catch(error => {
       if (error === "user.token.invalid") {
         console.log(`Invalid token`)
       } else {
         console.log(`Server error`)
       }
     })

    Parameters

    • token: string
    • Optional organizationId: undefined | string

    Returns Promise<JSessionData>