From b822e4d321cffbb3bec5de4a48888e11b0a47f4c Mon Sep 17 00:00:00 2001 From: Annika Merris Date: Fri, 5 Apr 2024 23:09:46 -0400 Subject: [PATCH] Implemented a Basic Pass Of Post-login Redirecting --- public/config.json | 2 +- src/components/GlobalHeader.vue | 23 ++++++++++++- src/main.ts | 6 ++-- src/router/index.ts | 60 ++++++++++++++++++++------------- src/stores/general.ts | 34 +++++++++++++++++++ src/types/ConfigSymbols.ts | 5 ++- src/views/LoginView.vue | 48 +++++++++++++------------- src/views/PostLoginView.vue | 10 ++++++ src/views/TestView.vue | 1 - 9 files changed, 135 insertions(+), 54 deletions(-) create mode 100644 src/stores/general.ts create mode 100644 src/views/PostLoginView.vue diff --git a/public/config.json b/public/config.json index 1c24583..e89a2f4 100644 --- a/public/config.json +++ b/public/config.json @@ -1,5 +1,5 @@ { - "apiBaseURL": "http://coder.local.merr.is:3000", + "apiBaseURL": "http://coder.local.merr.is:3001", "oidcAuthority": "https://auth.joes.moosenet.work", "oidcClientID": "255988227184328707@isekai:_slow_life_calculator", "oidcProjectID": "255987963094106115" diff --git a/src/components/GlobalHeader.vue b/src/components/GlobalHeader.vue index fd1edfb..4ae83ce 100644 --- a/src/components/GlobalHeader.vue +++ b/src/components/GlobalHeader.vue @@ -1,7 +1,22 @@ diff --git a/src/main.ts b/src/main.ts index 18da9f9..4ece5fa 100644 --- a/src/main.ts +++ b/src/main.ts @@ -24,7 +24,7 @@ const vuetify = createVuetify({ }) getConfig().then((conf: Config | null) => { - configureOidc().then((authentikAuth) => { + configureOidc().then((oidcAuth) => { if (conf === null) { throw new Error('config was null') } @@ -32,9 +32,9 @@ getConfig().then((conf: Config | null) => { app.provide(apiBaseURL, conf.apiBaseURL) - authentikAuth.startup().then((ok: boolean) => { + oidcAuth.startup().then((ok: boolean) => { if (ok) { - app.provide(oidc, authentikAuth) + app.provide(oidc, oidcAuth) const pinia = createPinia() diff --git a/src/router/index.ts b/src/router/index.ts index 0836a91..daa4c0d 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -1,6 +1,7 @@ import { configureOidc } from '@/services/authentikAuth' import { getConfig } from '@/services/siteConfig' import type { Config } from '@/types/Config' +import { LOGIN_RETURN_URL_STORAGE } from '@/types/ConfigSymbols' import type { OidcAuth } from 'vue-oidc-client/vue3' import { createRouter, createWebHistory } from 'vue-router' @@ -34,30 +35,43 @@ const router = createRouter({ path: '/intimacy-power', name: 'intimacy-power', component: () => import('@/views/IntimacyPowerView.vue') - } - // { - // path: '/test', - // name: 'test', - // component: () => import('@/views/TestView.vue') - // }, + }, + { + path: '/test', + name: 'test', + component: () => + hasRole('admin') ? import('@/views/TestView.vue') : import('@/views/NoAccess.vue') + }, - // { - // path: '/login', - // name: 'login', - // meta: { - // authName: oidcAuth.authName - // }, - // component: () => import('@/views/LoginView.vue') - // }, - // { - // path: '/admin', - // name: 'admin', - // meta: { - // authName: oidcAuth.authName - // }, - // component: () => - // hasRole('admin') ? import('@/views/Admin.vue') : import('@/views/NoAccess.vue') - // } + { + path: '/login', + name: 'login', + meta: { + authName: oidcAuth.authName + }, + component: () => import('@/views/LoginView.vue') + }, + { + path: '/admin', + name: 'admin', + meta: { + authName: oidcAuth.authName + }, + component: () => + hasRole('admin') ? import('@/views/Admin.vue') : import('@/views/NoAccess.vue') + }, + { + path: '/postLogin', + name: 'postLogin', + redirect: () => { + let redirectUrl = sessionStorage.getItem(LOGIN_RETURN_URL_STORAGE) + if (redirectUrl === "" || redirectUrl === null) { + redirectUrl = "/" + } + sessionStorage.removeItem(LOGIN_RETURN_URL_STORAGE) + return { path: redirectUrl } + } + } ] }) diff --git a/src/stores/general.ts b/src/stores/general.ts new file mode 100644 index 0000000..945062e --- /dev/null +++ b/src/stores/general.ts @@ -0,0 +1,34 @@ +import { defineStore } from 'pinia' +import { ref } from 'vue' + +export const useGeneralState = defineStore('general', () => { + const LOGIN_RETURN_URL_STORAGE = 'LOGIN_RETURN_URL_STORAGE' + + const loginReturnUrl = ref("") + + function setLoginReturnUrl(url: string) { + loginReturnUrl.value = url + localStorage.setItem( + LOGIN_RETURN_URL_STORAGE, + loginReturnUrl.value, + ) + } + + function getLoginReturnUrl(): string { + const storedUrl = localStorage.getItem(LOGIN_RETURN_URL_STORAGE) + if (storedUrl === null || storedUrl === undefined || storedUrl === "") { + localStorage.removeItem(LOGIN_RETURN_URL_STORAGE) + const res = loginReturnUrl.value + loginReturnUrl.value == null + return res + } + localStorage.removeItem(LOGIN_RETURN_URL_STORAGE) + loginReturnUrl.value == null + return storedUrl + } + + return { + setLoginReturnUrl, + getLoginReturnUrl + } +}) \ No newline at end of file diff --git a/src/types/ConfigSymbols.ts b/src/types/ConfigSymbols.ts index 3d00a04..0b9c80c 100644 --- a/src/types/ConfigSymbols.ts +++ b/src/types/ConfigSymbols.ts @@ -5,8 +5,11 @@ const apiBaseURL = Symbol() as InjectionKey const oidcProjectID = Symbol() as InjectionKey const oidc = Symbol() as InjectionKey +const LOGIN_RETURN_URL_STORAGE = "LOGIN_RETURN_URL_STORAGE" + export { apiBaseURL, oidcProjectID, - oidc + oidc, + LOGIN_RETURN_URL_STORAGE } \ No newline at end of file diff --git a/src/views/LoginView.vue b/src/views/LoginView.vue index 93d3724..f0fe530 100644 --- a/src/views/LoginView.vue +++ b/src/views/LoginView.vue @@ -27,37 +27,37 @@ } } - diff --git a/src/views/PostLoginView.vue b/src/views/PostLoginView.vue new file mode 100644 index 0000000..3f9cbf3 --- /dev/null +++ b/src/views/PostLoginView.vue @@ -0,0 +1,10 @@ + + + diff --git a/src/views/TestView.vue b/src/views/TestView.vue index 2da0bb4..b5b51fd 100644 --- a/src/views/TestView.vue +++ b/src/views/TestView.vue @@ -1,6 +1,5 @@