88 lines
2.6 KiB
JavaScript
88 lines
2.6 KiB
JavaScript
/**
|
|
* Configure oidc-client-ts library and retrieve UserManager instance for authentication.
|
|
*
|
|
* The documentation of oidc-client-ts is very...thin; this might be helpful:
|
|
* <https://gist.github.com/davidamidon/24c8a6980e116e62f781be4d6239d10d>
|
|
*
|
|
*/
|
|
|
|
import { Log, UserManager, WebStorageStateStore } from 'oidc-client-ts';
|
|
|
|
Log.logger = console;
|
|
Log.level = (import.meta.env.PROD) ? Log.ERROR : Log.DEBUG;
|
|
|
|
/**
|
|
* OpenID Connect Config
|
|
* Fields can be overridden by environment variables. All variables start with a VITE_BB_OIDC_ prefix.
|
|
*/
|
|
const oidcProviderDomain = import.meta.env.VITE_BB_OIDC_DOMAIN || 'https://basebox-test-1.eu.auth0.com';
|
|
const clientId = import.meta.env.VITE_BB_OIDC_CLIENT_ID || '5wl8hQV1thh07rScSoJ3aN56ETuXWprg';
|
|
const clientSecret = import.meta.env.VITE_BB_OIDC_SECRET || 'QlHMvIffKLRviCcSu_bPQcf8e4dc6WeS3BwZE1r1F-9R30AFoeYEwaOazAuFenI5';
|
|
const scopes = import.meta.env.VITE_BB_OIDC_SCOPES || "openid profile email name nickname";
|
|
export const callbackPath = import.meta.env.VITE_BB_OIDC_CALLBACK_PATH || "/auth/callback"
|
|
|
|
/* OIDC UserManager singleton */
|
|
let userMgr = null;
|
|
|
|
/**
|
|
* Get OIDC UserManager singleton.
|
|
*
|
|
* @returns OIDC UserManager singleton.
|
|
*/
|
|
export const getOidcUserManager = () => {
|
|
if (userMgr) {
|
|
return userMgr;
|
|
}
|
|
|
|
userMgr = new UserManager({
|
|
userStore: new WebStorageStateStore(),
|
|
authority: oidcProviderDomain,
|
|
client_id: clientId,
|
|
client_secret: clientSecret,
|
|
redirect_uri: window.location.origin + callbackPath,
|
|
response_type: 'code',
|
|
response_mode: 'query',
|
|
scope: scopes,
|
|
post_logout_redirect_uri: window.location.origin,
|
|
accessTokenExpiringNotificationTime: 10,
|
|
automaticSilentRenew: false,
|
|
filterProtocolClaims: false,
|
|
loadUserInfo: true,
|
|
includeIdTokenInSilentRenew: false
|
|
});
|
|
|
|
userMgr.events.addAccessTokenExpiring(function() {
|
|
console.info('OIDC: access token expiring')
|
|
})
|
|
|
|
userMgr.events.addAccessTokenExpired(function() {
|
|
console.info('OIDC: access token expired')
|
|
})
|
|
|
|
userMgr.events.addSilentRenewError(function(err) {
|
|
console.error('silent renew error', err)
|
|
})
|
|
|
|
userMgr.events.addUserLoaded(function(user) {
|
|
console.info('OIDC: user loaded', user)
|
|
})
|
|
|
|
userMgr.events.addUserSignedIn(function(user) {
|
|
console.info('OIDC: user signed in', user)
|
|
})
|
|
|
|
userMgr.events.addUserUnloaded(function() {
|
|
console.info('OIDC: user unloaded')
|
|
})
|
|
|
|
userMgr.events.addUserSignedOut(function() {
|
|
console.info('OIDC: user signed out')
|
|
})
|
|
|
|
userMgr.events.addUserSessionChanged(function() {
|
|
console.info('OIDC: user session changed')
|
|
})
|
|
|
|
return userMgr;
|
|
}
|