Skip to content

Commit

Permalink
- moved Business ID code after config.json code so we can redirect to…
Browse files Browse the repository at this point in the history
… the proper Businesses Registry home page (#51)

- deleted obsolete config file
  • Loading branch information
severinbeauvais authored Mar 26, 2020
1 parent ed386a2 commit c73702a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 39 deletions.
10 changes: 0 additions & 10 deletions public/config/local-configuration.json

This file was deleted.

15 changes: 9 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ async function start () {
// execution and error handling
start().catch((error) => {
console.error(error) // eslint-disable-line no-console
// no alert at this time
// just redirect to BC Registry home page
// NB: this is a hard-coded URL because we are probably missing the config keys
const bcRegUrl = 'https://www.bcregistry.ca/cooperatives/auth/'
// assume BC Registry URL is always reachable
window.location.assign(bcRegUrl)
// try to redirect to Business Registry home page
const businessesUrl = sessionStorage.getItem('BUSINESSES_URL')
if (businessesUrl) {
// assume Businesses URL is always reachable
window.location.assign(businessesUrl)
} else {
alert('There was an error starting this page. (See console for details.)\n' +
'Please try again later.')
}
})
46 changes: 23 additions & 23 deletions src/utils/fetch-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,13 @@ export async function fetchConfig (): Promise<void> {
//
const processEnvVueAppPath: string = process.env.VUE_APP_PATH // eg, cooperatives
const processEnvBaseUrl: string = process.env.BASE_URL // eg, /cooperatives/
const windowLocationPathname: string = window.location.pathname // eg, /cooperatives/CP0000841/...
const windowLocationOrigin: string = window.location.origin // eg, http://localhost:8080
const windowLocationPathname = window.location.pathname // eg, /cooperatives/CP0000841/...
const windowLocationOrigin = window.location.origin // eg, http://localhost:8080

if (!processEnvVueAppPath || !processEnvBaseUrl || !windowLocationPathname || !windowLocationOrigin) {
return Promise.reject(new Error('Missing environment variables'))
}

// get Business ID and validate that it looks OK
// it should be first token after Base URL in Pathname
// FUTURE: improve Business ID validation
const businessId = windowLocationPathname.replace(processEnvBaseUrl, '').split('/', 1)[0]
if (!businessId?.startsWith('CP') && !businessId?.startsWith('BC') && !businessId?.startsWith('NR')) {
return Promise.reject(new Error('Missing or invalid Business ID.'))
}
sessionStorage.setItem('BUSINESS_ID', businessId)

// set Base for Vue Router
// eg, /cooperatives/CP0000841/
const vueRouterBase = processEnvBaseUrl + businessId + '/'
sessionStorage.setItem('VUE_ROUTER_BASE', vueRouterBase)
console.log('Set Vue Router Base to: ' + vueRouterBase)

// set Base URL for returning from redirects
// eg, http://localhost:8080/cooperatives/CP0000841/
const baseUrl = windowLocationOrigin + vueRouterBase
sessionStorage.setItem('BASE_URL', baseUrl)
console.log('Set Base URL to: ' + baseUrl)

//
// fetch config from API
//
Expand Down Expand Up @@ -86,4 +65,25 @@ export async function fetchConfig (): Promise<void> {
const ldClientId = response.data['LD_CLIENT_ID']
window['ldClientId'] = ldClientId
console.info('Set Launch Darkly Client ID.')

// get Business ID and validate that it looks OK
// it should be first token after Base URL in Pathname
// FUTURE: improve Business ID validation
const businessId = windowLocationPathname.replace(processEnvBaseUrl, '').split('/', 1)[0]
if (!businessId?.startsWith('CP') && !businessId?.startsWith('BC') && !businessId?.startsWith('NR')) {
return Promise.reject(new Error('Missing or invalid Business ID.'))
}
sessionStorage.setItem('BUSINESS_ID', businessId)

// set Base for Vue Router
// eg, /cooperatives/CP0000841/
const vueRouterBase = processEnvBaseUrl + businessId + '/'
sessionStorage.setItem('VUE_ROUTER_BASE', vueRouterBase)
console.log('Set Vue Router Base to: ' + vueRouterBase)

// set Base URL for returning from redirects
// eg, http://localhost:8080/cooperatives/CP0000841/
const baseUrl = windowLocationOrigin + vueRouterBase
sessionStorage.setItem('BASE_URL', baseUrl)
console.log('Set Base URL to: ' + baseUrl)
}

0 comments on commit c73702a

Please sign in to comment.