75 lines
2 KiB
TypeScript
75 lines
2 KiB
TypeScript
import { createApp } from 'vue'
|
|
import Keycloak from "keycloak-js"
|
|
import VueKeycloakJs from '@dsb-norge/vue-keycloak-js'
|
|
import axios, { type AxiosInstance } from 'axios'
|
|
import App from './App.vue'
|
|
|
|
interface Settings {
|
|
keycloakUri: string
|
|
realm: string
|
|
clientId: string
|
|
sso: boolean
|
|
resourceServerUrl: string
|
|
resourceScopes: string[]
|
|
authProvider: string
|
|
tokenSandbox: boolean
|
|
}
|
|
|
|
|
|
export let settings: Settings
|
|
export let authServer: AxiosInstance
|
|
export let resourceServer: AxiosInstance
|
|
|
|
// The settings.json file is expected at the server's base url
|
|
axios.get("settings.json").then().then(
|
|
resp => {
|
|
settings = resp.data
|
|
resourceServer = axios.create({
|
|
baseURL: settings.resourceServerUrl,
|
|
timeout: 10000
|
|
})
|
|
authServer = axios.create({
|
|
baseURL: '/',
|
|
timeout: 10000
|
|
})
|
|
app.use(VueKeycloakJs, {
|
|
config: {
|
|
url: settings.keycloakUri,
|
|
realm: settings.realm,
|
|
clientId: settings.clientId
|
|
},
|
|
init: {
|
|
onLoad: settings.sso ? "check-sso" : "login-required",
|
|
scope: "openid email profile " + settings.resourceScopes.join(" ")
|
|
},
|
|
onReady(keycloak: Keycloak) {
|
|
initializeTokenInterceptor(keycloak)
|
|
},
|
|
})
|
|
app.mount("#app")
|
|
}
|
|
)
|
|
|
|
|
|
function initializeTokenInterceptor(keycloak: Keycloak) {
|
|
authServer.interceptors.request.use(axiosSettings => {
|
|
if (keycloak.authenticated) {
|
|
axiosSettings.headers.Authorization = `Bearer ${keycloak.token}`
|
|
axiosSettings.headers.auth_provider = settings.authProvider
|
|
}
|
|
return axiosSettings
|
|
}, error => {
|
|
return Promise.reject(error)
|
|
})
|
|
resourceServer.interceptors.request.use(axiosSettings => {
|
|
if (keycloak.authenticated) {
|
|
axiosSettings.headers.Authorization = `Bearer ${keycloak.token}`
|
|
axiosSettings.headers.auth_provider = settings.authProvider
|
|
}
|
|
return axiosSettings
|
|
}, error => {
|
|
return Promise.reject(error)
|
|
})
|
|
}
|
|
|
|
const app = createApp(App)
|