diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts index c87c88d2..4287ab74 100644 --- a/frontend/src/router/index.ts +++ b/frontend/src/router/index.ts @@ -14,6 +14,7 @@ import UserHomePage from "@/views/homepage/UserHomePage.vue"; import SingleTheme from "@/views/SingleTheme.vue"; import LearningObjectView from "@/views/learning-paths/learning-object/LearningObjectView.vue"; import authService from "@/services/auth/auth-service"; +import {allowRedirect, Redirect} from "@/utils/redirect.ts"; const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), @@ -144,10 +145,10 @@ router.beforeEach(async (to, _from, next) => { if (to.meta.requiresAuth) { if (!authService.isLoggedIn.value && !(await authService.loadUser())) { const path = to.fullPath; - if (path !== "/") { - localStorage.setItem("redirectAfterLogin", path); + if (allowRedirect(path)) { + localStorage.setItem(Redirect.AFTER_LOGIN_KEY, path); } - next("/login"); + next(Redirect.LOGIN); } else { next(); } diff --git a/frontend/src/utils/redirect.ts b/frontend/src/utils/redirect.ts new file mode 100644 index 00000000..8ddefc87 --- /dev/null +++ b/frontend/src/utils/redirect.ts @@ -0,0 +1,14 @@ +export enum Redirect { + AFTER_LOGIN_KEY = "redirectAfterLogin", + HOME = "/user", + LOGIN = "/login", + ROOT = "/" +} + +const NOT_ALLOWED_REDIRECTS = new Set([ + Redirect.HOME, Redirect.ROOT, Redirect.LOGIN +]); + +export function allowRedirect(path: string): boolean { + return !NOT_ALLOWED_REDIRECTS.has(path as Redirect); +} diff --git a/frontend/src/views/CallbackPage.vue b/frontend/src/views/CallbackPage.vue index d07a3003..92de9e9f 100644 --- a/frontend/src/views/CallbackPage.vue +++ b/frontend/src/views/CallbackPage.vue @@ -3,6 +3,7 @@ import { useI18n } from "vue-i18n"; import { onMounted, ref, type Ref } from "vue"; import auth from "../services/auth/auth-service.ts"; + import {Redirect} from "@/utils/redirect.ts"; const { t } = useI18n(); @@ -11,12 +12,12 @@ const errorMessage: Ref = ref(null); async function redirectPage(): Promise { - const redirectUrl = localStorage.getItem("redirectAfterLogin"); + const redirectUrl = localStorage.getItem(Redirect.AFTER_LOGIN_KEY); if (redirectUrl) { - localStorage.removeItem("redirectAfterLogin"); + localStorage.removeItem(Redirect.AFTER_LOGIN_KEY); await router.replace(redirectUrl); } else { - await router.replace("/user"); // Redirect to theme page + await router.replace(Redirect.HOME); } }