Skip to main content

Utility Module

Utility Module provides some common methods that can be shared acrosss all micro apps inside control panel v2

To use methods from the utility module, simply import the methods you need:

import { getAuthToken, getCurrentUserToken, getCurrentUserPermissions } from '@tbm/microsite.utilities'

getAuthToken

getAuthToken()

  • Returns: string
  • Returns the current user's CP token, it always returns the valid token as we will refresh the token proactively
// import the method
import { getAuthToken } from '@tbm/microsite.utilities'

// put it where you need the token, e.g. before web api call

axios.interceptors.request.use(config => {
// get the latest token from utility module
const token = getAuthToken()
config.headers.Authorization =`Bearer ${token}`

return config
}, err => Promise.reject(err))

getCurrentUserToken

getCurrentUserToken(forceRefresh? = false)

  • Params: forceRefresh?: boolean set to true if you know user's token info changed, e.g. in price manager when swtich client, the user's custom claims changed, has to force to refetch the token to get the new custom claims from token. Otherwise better not to force refresh as it would be slower process
  • Returns: Promise<string>
  • Returns the current token if it has not expired or if it will not expire in the next five minutes. Otherwise, this will refresh the token and return a new one.
// import the method
import { getCurrentUserToken } from '@tbm/microsite.utilities'

// put it where you need the token, e.g. before web api call

axios.interceptors.request.use(async config => {
// get the latest token from utility module
const token = await getCurrentUserToken()
config.headers.Authorization =`Bearer ${token}`

return config
}, err => Promise.reject(err))

getCurrentUserPermissionsForApp

getCurrentUserPermissionsForApp(appName)

  • Params: appName: string You have to provide your micro app's serviceName to get the user's permissions for your app
  • Returns an object contains current logged in user's permissions
{
"permissions": [
{
"id": 2,
"policyType": "p",
"subject": "BetDirectorUsers",
"domain": "controlpanel",
"object": "betdirector",
"action": "read",
"effect": "allow"
},
{
"id": 4,
"policyType": "p",
"subject": "Administrator",
"domain": "controlpanel",
"object": "*",
"action": "write",
"effect": "allow"
},
{
"id": 14,
"policyType": "p",
"subject": "PriceManagerUsers",
"domain": "controlpanel",
"object": "pricemanager",
"action": "read",
"effect": "allow"
},
{
"id": 15,
"policyType": "p",
"subject": "RatingsManagerUsers",
"domain": "controlpanel",
"object": "ratingsmanager",
"action": "read",
"effect": "allow"
},
{
"id": 17,
"policyType": "p",
"subject": "BetDirectorUsers",
"domain": "controlpanel",
"object": "betdirectorui",
"action": "read",
"effect": "allow"
},
{
"id": 18,
"policyType": "p",
"subject": "BetDirectorUsers",
"domain": "controlpanel",
"object": "betdirector-audit",
"action": "read",
"effect": "allow"
}
]
}

isSingleModuleURL

isSingleModuleURL()

  • Returns a boolean value to indicates if it's in single module URL or not

signOutUser

signOutUser()

  • Use this method to sign out your current user, e.g your app is in single module URL, your user won't be bale to see left nav bar to sign out, so you might need to implement your own sign out button and call this method

More to Come...