refactor: constanten redirect vervangen door enum
This commit is contained in:
parent
4600e5dd9e
commit
064897acd5
3 changed files with 22 additions and 6 deletions
|
@ -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();
|
||||
}
|
||||
|
|
14
frontend/src/utils/redirect.ts
Normal file
14
frontend/src/utils/redirect.ts
Normal file
|
@ -0,0 +1,14 @@
|
|||
export enum Redirect {
|
||||
AFTER_LOGIN_KEY = "redirectAfterLogin",
|
||||
HOME = "/user",
|
||||
LOGIN = "/login",
|
||||
ROOT = "/"
|
||||
}
|
||||
|
||||
const NOT_ALLOWED_REDIRECTS = new Set<Redirect>([
|
||||
Redirect.HOME, Redirect.ROOT, Redirect.LOGIN
|
||||
]);
|
||||
|
||||
export function allowRedirect(path: string): boolean {
|
||||
return !NOT_ALLOWED_REDIRECTS.has(path as Redirect);
|
||||
}
|
|
@ -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<string | null> = ref(null);
|
||||
|
||||
async function redirectPage(): Promise<void> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue