// Requires: // enironment.prod.js // oidc-client.min.js // locale.js Oidc.Log.logger = console; let userManager; init(); function init() { try { // The exports.authenticationSettings global variable will be imported from environment.prod.js const authSettings = parseAuthSettings(exports.authenticationSettings); console.debug(authSettings); userManager = new Oidc.UserManager(authSettings); userManager.getUser() .then(function(user) { if (user) { redirectLoggedInUser(user); } else { startAuthentication(); } }) .catch(function(e){ showMessage(e.message); }); } catch(e) { console.error(e); showMessage('Unable to initialise authentication provider'); return; } } function redirectLoggedInUser(user) { let locale; // Redirect to the root of the localised Angular app let pathParts = document.location.pathname.split('/'); if (pathParts.length > 1) locale = pathParts[1]; let routeToUrl = document.location.href; if (!isValidLocale(locale)) { // Locale is invalid. Find the best match. const invalidLocale = locale; locale = getLocaleForUser(user); // Replace the locale in the routeToUrl routeToUrl = document.location.href.replace(invalidLocale, locale); } // Store the requested URL so Angular can route to it window.sessionStorage.setItem('dimensions.postAuthenticationRedirectUrl', routeToUrl); // Redirect to the approprirate Angular app let angularBaseUrl = `${document.location.origin}/${locale}`; console.debug('Redirect logged-in user to', angularBaseUrl); document.location.replace(angularBaseUrl); } function parseAuthSettings(settings) { let parsedSettings = { }; for(var key in settings) { let value = settings[key]; if (typeof value === 'string') { value = value.replace('::origin::', window.location.origin); } parsedSettings[key] = value; } return parsedSettings; } function showMessage(message) { document.getElementById('message').innerText = message; } function startAuthentication(args) { console.debug('startAuthentication'); let url = document.location.href; console.debug('Set post-auth URL', url); window.sessionStorage.setItem('dimensions.postAuthenticationRedirectUrl', url); userManager.signinRedirect(args) .catch(e => { console.error('Error starting authentication', e); showMessage("Unable to connect to authentication server."); }); }