-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathmain.ts
153 lines (118 loc) · 4.6 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { createHead } from '@unhead/vue'
import { createApp } from 'vue'
import piniaInstance, { useAppStore } from '@/stores'
import App from './App.vue'
import { portalRouter } from './router'
import { removeQueryParam } from './router/route-utils'
import useLaunchDarkly from '@/composables/useLaunchDarkly'
import { authApi, authApiBaseUrl, session } from '@/services'
// Import kong-auth-elements, styles, and options interface
import { KongAuthElementsPlugin } from '@kong/kong-auth-elements/dist/kong-auth-elements.es'
import '@kong/kong-auth-elements/dist/style.css'
import handleKongAuthElementsError from '@/helpers/handleKongAuthElementsError'
// Globally import all Kongponents
import Kongponents from '@kong/kongponents'
import '@kong/kongponents/dist/style.css'
import './assets/utilities.scss'
import './main.css'
// Globally defined components
import { registerComponents } from './components/registerComponents'
import CopyUuid, { CopyUuidNotifyParam } from '@kong-ui-public/copy-uuid'
import '@kong-ui-public/copy-uuid/dist/style.css'
import useToaster from './composables/useToaster'
import usePortalApi from './hooks/usePortalApi'
import { createRedirectHandler } from './helpers/auth'
import portalAnalyticsBridge from '@kong-ui-public/portal-analytics-bridge'
import { PortalContext } from '@kong/sdk-portal-js'
/**
* Initialize application
*/
async function init () {
const app = createApp(App)
const head = createHead()
app.use(head)
// Initialize the Pinia store
app.use(piniaInstance)
const router = portalRouter()
const { setPortalData, setSession, logout } = useAppStore()
authApi.setAuthErrorCallback(createRedirectHandler(router, logout))
app.use(Kongponents)
registerComponents(app)
const { portalApiV2 } = usePortalApi()
try {
const portalContext = await portalApiV2.value.service.portalApi.getPortalContext()
const {
portal_id: portalId,
org_id: orgId,
featureset_id: featuresetId,
feature_set: featureSet,
oidc_auth_enabled: oidcAuthEnabled,
saml_auth_enabled: samlAuthEnabled,
is_public: isPublic,
basic_auth_enabled: basicAuthEnabled,
rbac_enabled: isRbacEnabled,
allowed_time_period: allowedTimePeriod,
canonical_domain: canonicalDomain
} = portalContext.data as PortalContext & { saml_auth_enabled: boolean }
if (isPublic === false) {
portalApiV2.value.updateClientWithCredentials()
}
// SAML Auth enabled comes on a different portal context property, but is handled the same as OIDC by the Auth Client
const authClientConfig = { basicAuthEnabled, oidcAuthEnabled: oidcAuthEnabled || samlAuthEnabled }
setPortalData({ portalId, orgId, authClientConfig, featuresetId, featureSet, isPublic, isRbacEnabled, allowedTimePeriod, canonicalDomain })
setSession(session)
// Fetch session data from localStorage
await session.saveData(session.checkLocalDataForUser())
const { initialize: initLaunchDarkly } = useLaunchDarkly()
await initLaunchDarkly()
app.use(portalAnalyticsBridge, {
apiClient: portalApiV2.value.service.applicationAnalyticsApi
})
if (!isPublic) {
if (session.authenticatedWithIdp()) {
let res
try {
res = await portalApiV2.value.service.developerApi.getDeveloperMe()
} catch (e) {
// // catch error to prevent going directly to global api error handler
res = { data: undefined }
// remove loginSuccess to adjust session check
removeQueryParam('loginSuccess')
}
await session.saveData({
...session.data,
developer: res.data
})
}
}
app.use(router)
// Register the kong-auth-elements Vue plugin
app.use(KongAuthElementsPlugin, {
apiBaseUrl: authApiBaseUrl,
userEntity: 'developer',
shadowDom: false,
customErrorHandler: handleKongAuthElementsError,
developerConfig: {
portalId
}
})
app.use(CopyUuid, {
notify: (props: CopyUuidNotifyParam) => {
useToaster().notify({
appearance: props.type === 'error' ? 'danger' : 'success',
message: props.message
})
}
})
app.mount('#app')
} catch (error) {
// This logic ensures that if the portalaccesstoken is invalid
// a user will not get stuck on the loading screen
if (error?.response?.status === 401 && window.location.hostname.includes('localhost')) {
document.cookie = `portalaccesstoken=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; domain=${window.location.hostname};`
session.destroy()
window.location.reload()
}
}
}
init()