From ea8f6188aa936024a34d3364e04081bc3d78bcdd Mon Sep 17 00:00:00 2001 From: Werner Kramer Date: Wed, 13 Dec 2023 21:06:08 +0100 Subject: [PATCH 1/7] Remove authorize meta property --- src/router/index.ts | 47 +++++++++++++++++++-------------------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/src/router/index.ts b/src/router/index.ts index 70f9c4115..2b9251b21 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -38,7 +38,6 @@ const routesBase: Readonly = [ { path: '/about', name: 'About', - meta: { authorize: [] }, component: AboutView, }, { @@ -49,16 +48,19 @@ const routesBase: Readonly = [ }, { path: '/auth/callback', + name: 'AuthCallback', meta: { layout: 'EmptyLayout' }, component: Callback, }, { path: '/auth/silent', + name: 'AuthSilent', meta: { layout: 'EmptyLayout' }, component: Silent, }, { path: '/auth/logout', + name: 'AuthLogout', meta: { layout: 'EmptyLayout' }, component: Logout, }, @@ -82,14 +84,14 @@ export const dynamicRoutes: Readonly = [ name: 'SchematicStatusDisplay', component: SchematicStatusDisplayView, props: true, - meta: { authorize: [], sidebar: true }, + meta: { sidebar: true }, children: [ { path: '/ssd/:groupId?/:panelId?/:objectId', name: 'SSDTimeSeriesDisplay', component: SSDTimeSeriesDisplay, props: true, - meta: { authorize: [], sidebar: true }, + meta: { sidebar: true }, }, ], }, @@ -97,42 +99,41 @@ export const dynamicRoutes: Readonly = [ path: '/systemmonitor', name: 'SystemMonitor', component: SystemMonitorDisplayView, - meta: { authorize: [] }, }, { path: '/map/:layerName?', name: 'SpatialDisplay', component: SpatialDisplayView, props: true, - meta: { authorize: [], sidebar: true }, + meta: { sidebar: true }, }, { path: '/series/node/:nodeId?', name: 'TimeSeriesDisplay', component: TimeSeriesDisplayView, props: true, - meta: { authorize: [], sidebar: true }, + meta: { sidebar: true }, }, { path: '/topology/node/:nodeId?', name: 'TopologyDisplay', component: TopologyDisplayView, props: true, - meta: { authorize: [], sidebar: true }, + meta: { sidebar: true }, children: [ { path: '/topology/node/:nodeId?/series/', name: 'TopologyTimeSeries', component: TimeSeriesDisplay, props: true, - meta: { authorize: [], sidebar: true }, + meta: { sidebar: true }, }, { path: '/topology/node/:nodeId?/map/:layerName?', name: 'TopologySpatialDisplay', component: SpatialDisplay, props: true, - meta: { authorize: [], sidebar: true }, + meta: { sidebar: true }, }, ], }, @@ -140,7 +141,6 @@ export const dynamicRoutes: Readonly = [ path: '/archivedisplay', name: 'ArchiveDisplay', component: Empty, - meta: { authorize: [] }, }, ] @@ -153,21 +153,11 @@ let routesAreInitialized = false async function handleAuthorization( to: RouteLocationNormalized, - authorize: string[], ) { const currentUser = await authenticationManager.userManager.getUser() if (currentUser === null) { return { name: 'Login', query: { redirect: to.path } } } - - const role = - currentUser.profile.roles !== undefined - ? (currentUser.profile as any).roles[0] - : 'guest' - - if (authorize.length && !authorize.includes(role)) { - return { name: 'About' } - } } async function addDynamicRoutes() { @@ -204,23 +194,26 @@ function fillRouteParams(to: RouteLocationNormalized) { } router.beforeEach(async (to, _from) => { - const authorize = to.meta?.authorize as string[] - if (authorize && configManager.authenticationIsEnabled) { - const authPath = await handleAuthorization(to, authorize) - if (authPath) return authPath - } - - if (to.path === '/auth/callback') { + if (to.name === 'Login' || to.name === 'AuthSilent' || to.name === 'AuthLogout') return + if (to.name === 'AuthCallback') { try { const user = await authenticationManager.userManager.signinRedirectCallback() + console.log('callback user: ', user) const path: string = user.state === null ? '/' : (user.state as string) + console.log('Redirecting to: ' + path) return { path } } catch (error) { console.error(error) + return { name: 'Default' } } } + if (configManager.authenticationIsEnabled) { + const authPath = await handleAuthorization(to) + if (authPath) return authPath + } + if (!routesAreInitialized) { await addDynamicRoutes() routesAreInitialized = true From c6bbbf68793b158369d032354a02e0a07e0189ec Mon Sep 17 00:00:00 2001 From: Werner Kramer Date: Wed, 13 Dec 2023 21:06:53 +0100 Subject: [PATCH 2/7] Show correct compent name --- src/views/auth/Callback.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/views/auth/Callback.vue b/src/views/auth/Callback.vue index 3dfe4e52c..aa120f799 100644 --- a/src/views/auth/Callback.vue +++ b/src/views/auth/Callback.vue @@ -1,3 +1,3 @@ - + From 2d46f02568cbfb039fc285c01e76a9f6d3f7651a Mon Sep 17 00:00:00 2001 From: Werner Kramer Date: Wed, 13 Dec 2023 21:08:00 +0100 Subject: [PATCH 3/7] Add object in SSDTimeSeriesDisplay route --- src/router/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/router/index.ts b/src/router/index.ts index 2b9251b21..f5a807653 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -87,7 +87,7 @@ export const dynamicRoutes: Readonly = [ meta: { sidebar: true }, children: [ { - path: '/ssd/:groupId?/:panelId?/:objectId', + path: '/ssd/:groupId?/:panelId?/object/:objectId', name: 'SSDTimeSeriesDisplay', component: SSDTimeSeriesDisplay, props: true, From 3a29b1372e30ecb74c59e3fc03b433aa899af598 Mon Sep 17 00:00:00 2001 From: Werner Kramer Date: Wed, 13 Dec 2023 21:34:42 +0100 Subject: [PATCH 4/7] Fix redirect --- src/router/index.ts | 17 +++++++---------- src/views/auth/DeltaresLogin.vue | 2 +- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/router/index.ts b/src/router/index.ts index f5a807653..0926fb248 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -156,7 +156,7 @@ async function handleAuthorization( ) { const currentUser = await authenticationManager.userManager.getUser() if (currentUser === null) { - return { name: 'Login', query: { redirect: to.path } } + return { name: 'Login', query: { redirect: to.redirectedFrom?.path ?? to.path } } } } @@ -193,19 +193,17 @@ function fillRouteParams(to: RouteLocationNormalized) { return to } -router.beforeEach(async (to, _from) => { - if (to.name === 'Login' || to.name === 'AuthSilent' || to.name === 'AuthLogout') return +router.beforeEach(async (to, from) => { + router.resolve(to.path) + let redirect = '/' + if (to.name === 'Login' || to.name === 'AuthLogout') return if (to.name === 'AuthCallback') { try { const user = await authenticationManager.userManager.signinRedirectCallback() - console.log('callback user: ', user) - const path: string = user.state === null ? '/' : (user.state as string) - console.log('Redirecting to: ' + path) - return { path } + if ( user.state ) redirect = user.state.toString() } catch (error) { console.error(error) - return { name: 'Default' } } } @@ -217,8 +215,7 @@ router.beforeEach(async (to, _from) => { if (!routesAreInitialized) { await addDynamicRoutes() routesAreInitialized = true - if (to.redirectedFrom) return to.redirectedFrom - return to + return redirect } to = fillRouteParams(to) diff --git a/src/views/auth/DeltaresLogin.vue b/src/views/auth/DeltaresLogin.vue index f597994a5..5d1b00e11 100644 --- a/src/views/auth/DeltaresLogin.vue +++ b/src/views/auth/DeltaresLogin.vue @@ -15,7 +15,7 @@ const props = defineProps({ const route = useRoute() function login(): void { - const redirect = route.query.redirect || '/about' + const redirect = route.query.redirect ?? '/' authenticationManager.userManager.signinRedirect({ state: redirect }) } From 4cccaaa5c1f5a9fa0d85b049c99b95b7e1f9d088 Mon Sep 17 00:00:00 2001 From: Werner Kramer Date: Wed, 13 Dec 2023 22:02:37 +0100 Subject: [PATCH 5/7] Fix when no authentication is used --- src/router/index.ts | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/router/index.ts b/src/router/index.ts index 0926fb248..9b5c526f1 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -181,21 +181,25 @@ async function addDynamicRoutes() { } } -function fillRouteParams(to: RouteLocationNormalized) { +function defaultRouteParams(to: RouteLocationNormalized) { const store = useConfigStore() - if (to.name) { - const component = store.getComponentByType(to.name as string) - if (component !== undefined) { - const defaultPath = component.defaultPath - to.params = { ...defaultPath, ...to.params } + const route = to.name === undefined ? router.resolve(to) : to + const component = store.getComponentByType(route.name as string) + if (component !== undefined) { + const defaultPath = component.defaultPath + const params = to.params + for (const key in defaultPath) { + if (defaultPath[key] !== undefined) { + params[key] = params[key] || defaultPath[key] + } } + return params } - return to + return to.params } router.beforeEach(async (to, from) => { - router.resolve(to.path) - let redirect = '/' + let redirect: string | undefined = undefined if (to.name === 'Login' || to.name === 'AuthLogout') return if (to.name === 'AuthCallback') { try { @@ -208,17 +212,22 @@ router.beforeEach(async (to, from) => { } if (configManager.authenticationIsEnabled) { - const authPath = await handleAuthorization(to) - if (authPath) return authPath + const redirectToLogin = await handleAuthorization(to) + if (redirectToLogin) return redirectToLogin } if (!routesAreInitialized) { await addDynamicRoutes() routesAreInitialized = true - return redirect + if (redirect) { + return redirect + } else if(to.path === '/about' && from.path === '/') { + return from + } else { + return to + } } - - to = fillRouteParams(to) + to.params = defaultRouteParams(to) return }) From 70fca6d05520874a291f237e35997d33695ae907 Mon Sep 17 00:00:00 2001 From: Werner Kramer Date: Wed, 13 Dec 2023 22:50:03 +0100 Subject: [PATCH 6/7] Formatting --- src/router/index.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/router/index.ts b/src/router/index.ts index 9b5c526f1..48411877a 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -151,12 +151,13 @@ const router = createRouter({ let routesAreInitialized = false -async function handleAuthorization( - to: RouteLocationNormalized, -) { +async function handleAuthorization(to: RouteLocationNormalized) { const currentUser = await authenticationManager.userManager.getUser() if (currentUser === null) { - return { name: 'Login', query: { redirect: to.redirectedFrom?.path ?? to.path } } + return { + name: 'Login', + query: { redirect: to.redirectedFrom?.path ?? to.path }, + } } } @@ -205,7 +206,7 @@ router.beforeEach(async (to, from) => { try { const user = await authenticationManager.userManager.signinRedirectCallback() - if ( user.state ) redirect = user.state.toString() + if (user.state) redirect = user.state.toString() } catch (error) { console.error(error) } @@ -221,7 +222,7 @@ router.beforeEach(async (to, from) => { routesAreInitialized = true if (redirect) { return redirect - } else if(to.path === '/about' && from.path === '/') { + } else if (to.path === '/about' && from.path === '/') { return from } else { return to From 4b937218346c65385ff2857a1279daef8da2c462 Mon Sep 17 00:00:00 2001 From: Werner Kramer Date: Wed, 13 Dec 2023 23:41:19 +0100 Subject: [PATCH 7/7] Do not redirect Default, unless configured --- src/router/index.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/router/index.ts b/src/router/index.ts index 48411877a..1cd24a31a 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -33,7 +33,7 @@ const routesBase: Readonly = [ { path: '/', name: 'Default', - redirect: 'About', + component: AboutView, }, { path: '/about', @@ -222,11 +222,8 @@ router.beforeEach(async (to, from) => { routesAreInitialized = true if (redirect) { return redirect - } else if (to.path === '/about' && from.path === '/') { - return from - } else { - return to } + return to } to.params = defaultRouteParams(to) return