Merge pull request #124 from SELab-2/feat/router-reinitialization

feat: Verbetering en simplificatie van de router
This commit is contained in:
Joyelle Ndagijimana 2025-03-18 09:34:26 +01:00 committed by GitHub
commit 933cb496e3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 89 additions and 64 deletions

View file

@ -1,27 +1,28 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref } from "vue"; import { ref } from "vue";
import { useRoute } from "vue-router";
import dwengoLogo from "../../../assets/img/dwengo-groen-zwart.svg";
import { useI18n } from "vue-i18n"; import { useI18n } from "vue-i18n";
const route = useRoute(); import auth from "@/services/auth/auth-service.ts";
// Import assets
import dwengoLogo from "../../../assets/img/dwengo-groen-zwart.svg";
const { t, locale } = useI18n(); const { t, locale } = useI18n();
// Instantiate variables to use in html to render right // Instantiate variables to use in html to render right
// Links and content dependent on the role (student or teacher) // Links and content dependent on the role (student or teacher)
const isTeacher = route.path.includes("teacher"); const path = "/user";
const userId = route.params.id as string; const role = auth.authState.activeRole;
const role = isTeacher ? "teacher" : "student"; //TODO: use authState form services map to get user token
const name = "Kurt Cobain"; const name = "Kurt Cobain";
const initials = name const initials = name
.split(" ") .split(" ")
.map((n) => { .map((n) => n[0])
return n[0];
})
.join(""); .join("");
// Available languages
const languages = ref([ const languages = ref([
{ name: "English", code: "en" }, { name: "English", code: "en" },
{ name: "Nederlands", code: "nl" }, { name: "Nederlands", code: "nl" },
@ -42,7 +43,7 @@
<ul> <ul>
<li> <li>
<router-link <router-link
:to="`/${role}/${userId}`" :to="`${path}`"
class="dwengo_home" class="dwengo_home"
> >
<img <img
@ -56,7 +57,7 @@
</li> </li>
<li> <li>
<router-link <router-link
:to="`/${role}/${userId}/assignment`" :to="`/user/assignment`"
class="menu_item" class="menu_item"
> >
{{ t("assignments") }} {{ t("assignments") }}
@ -64,14 +65,14 @@
</li> </li>
<li> <li>
<router-link <router-link
:to="`/${role}/${userId}/class`" :to="`${path}/class`"
class="menu_item" class="menu_item"
>{{ t("classes") }}</router-link >{{ t("classes") }}</router-link
> >
</li> </li>
<li> <li>
<router-link <router-link
:to="`/${role}/${userId}/discussion`" :to="`${path}/discussion`"
class="menu_item" class="menu_item"
>{{ t("discussions") }} >{{ t("discussions") }}
</router-link> </router-link>

View file

@ -1,13 +1,6 @@
import { createRouter, createWebHistory } from "vue-router"; import { createRouter, createWebHistory } from "vue-router";
import MenuBar from "@/components/MenuBar.vue"; import MenuBar from "@/components/MenuBar.vue";
import StudentHomepage from "@/views/StudentHomepage.vue"; import StudentHomepage from "@/views/homepage/StudentHomepage.vue";
import StudentAssignments from "@/views/assignments/StudentAssignments.vue";
import StudentClasses from "@/views/classes/StudentClasses.vue";
import StudentDiscussions from "@/views/discussions/StudentDiscussions.vue";
import TeacherHomepage from "@/views/TeacherHomepage.vue";
import TeacherAssignments from "@/views/assignments/TeacherAssignments.vue";
import TeacherClasses from "@/views/classes/TeacherClasses.vue";
import TeacherDiscussions from "@/views/discussions/TeacherDiscussions.vue";
import SingleAssignment from "@/views/assignments/SingleAssignment.vue"; import SingleAssignment from "@/views/assignments/SingleAssignment.vue";
import SingleClass from "@/views/classes/SingleClass.vue"; import SingleClass from "@/views/classes/SingleClass.vue";
import SingleDiscussion from "@/views/discussions/SingleDiscussion.vue"; import SingleDiscussion from "@/views/discussions/SingleDiscussion.vue";
@ -16,6 +9,10 @@ import CreateClass from "@/views/classes/CreateClass.vue";
import CreateAssignment from "@/views/assignments/CreateAssignment.vue"; import CreateAssignment from "@/views/assignments/CreateAssignment.vue";
import CreateDiscussion from "@/views/discussions/CreateDiscussion.vue"; import CreateDiscussion from "@/views/discussions/CreateDiscussion.vue";
import CallbackPage from "@/views/CallbackPage.vue"; import CallbackPage from "@/views/CallbackPage.vue";
import UserDiscussions from "@/views/discussions/UserDiscussions.vue";
import UserClasses from "@/views/classes/UserClasses.vue";
import UserAssignments from "@/views/classes/UserAssignments.vue";
import authState from "@/services/auth/auth-service.ts";
const router = createRouter({ const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL), history: createWebHistory(import.meta.env.BASE_URL),
@ -24,105 +21,104 @@ const router = createRouter({
path: "/", path: "/",
name: "home", name: "home",
component: () => import("../views/HomePage.vue"), component: () => import("../views/HomePage.vue"),
meta: { requiresAuth: false },
}, },
{ {
path: "/login", path: "/login",
name: "LoginPage", name: "LoginPage",
component: () => import("../views/LoginPage.vue"), component: () => import("../views/LoginPage.vue"),
meta: { requiresAuth: false },
}, },
{ {
path: "/callback", path: "/callback",
component: CallbackPage, component: CallbackPage,
meta: { requiresAuth: false },
}, },
{ {
path: "/student/:id", path: "/user",
component: MenuBar, component: MenuBar,
meta: { requiresAuth: true },
children: [ children: [
{ {
path: "home", path: "home",
name: "StudentHomePage", name: "UserHomePage",
component: StudentHomepage, component: StudentHomepage,
}, },
{ {
path: "assignment", path: "assignment",
name: "StudentAssignments", name: "UserAssignments",
component: StudentAssignments, component: UserAssignments,
}, },
{ {
path: "class", path: "class",
name: "StudentClasses", name: "UserClasses",
component: StudentClasses, component: UserClasses,
}, },
{ {
path: "discussion", path: "discussion",
name: "StudentDiscussions", name: "UserDiscussions",
component: StudentDiscussions, component: UserDiscussions,
}, },
], ],
}, },
{
path: "/teacher/:id",
component: MenuBar,
children: [
{
path: "home",
name: "TeacherHomepage",
component: TeacherHomepage,
},
{
path: "assignment",
name: "TeacherAssignments",
component: TeacherAssignments,
},
{
path: "class",
name: "TeacherClasses",
component: TeacherClasses,
},
{
path: "discussion",
name: "TeacherDiscussions",
component: TeacherDiscussions,
},
],
},
{ {
path: "/assignment/create", path: "/assignment/create",
name: "CreateAssigment", name: "CreateAssigment",
component: CreateAssignment, component: CreateAssignment,
meta: { requiresAuth: true },
}, },
{ {
path: "/assignment/:id", path: "/assignment/:id",
name: "SingleAssigment", name: "SingleAssigment",
component: SingleAssignment, component: SingleAssignment,
meta: { requiresAuth: true },
}, },
{ {
path: "/class/create", path: "/class/create",
name: "CreateClass", name: "CreateClass",
component: CreateClass, component: CreateClass,
meta: { requiresAuth: true },
}, },
{ {
path: "/class/:id", path: "/class/:id",
name: "SingleClass", name: "SingleClass",
component: SingleClass, component: SingleClass,
meta: { requiresAuth: true },
}, },
{ {
path: "/discussion/create", path: "/discussion/create",
name: "CreateDiscussion", name: "CreateDiscussion",
component: CreateDiscussion, component: CreateDiscussion,
meta: { requiresAuth: true },
}, },
{ {
path: "/discussion/:id", path: "/discussion/:id",
name: "SingleDiscussion", name: "SingleDiscussion",
component: SingleDiscussion, component: SingleDiscussion,
meta: { requiresAuth: true },
}, },
{ {
path: "/:catchAll(.*)", path: "/:catchAll(.*)",
name: "NotFound", name: "NotFound",
component: NotFound, component: NotFound,
meta: { requiresAuth: false },
}, },
], ],
}); });
router.beforeEach(async (to, from, next) => {
// Verify if user is logged in before accessing certain routes
if (to.meta.requiresAuth) {
if (!authState.isLoggedIn.value) {
next("/login");
} else {
next();
}
} else {
next();
}
});
export default router; export default router;

View file

@ -20,6 +20,15 @@ async function getUserManagers(): Promise<UserManagersForRoles> {
}; };
} }
/**
* Information about the current authentication state.
*/
const authState = reactive<AuthState>({
user: null,
accessToken: null,
activeRole: authStorage.getActiveRole() || null,
});
/** /**
* Load the information about who is currently logged in from the IDP. * Load the information about who is currently logged in from the IDP.
*/ */
@ -35,15 +44,6 @@ async function loadUser(): Promise<User | null> {
return user; return user;
} }
/**
* Information about the current authentication state.
*/
const authState = reactive<AuthState>({
user: null,
accessToken: null,
activeRole: authStorage.getActiveRole() || null,
});
const isLoggedIn = computed(() => authState.user !== null); const isLoggedIn = computed(() => authState.user !== null);
/** /**

View file

@ -0,0 +1,7 @@
<script setup lang="ts"></script>
<template>
<main></main>
</template>
<style scoped></style>

View file

@ -0,0 +1,7 @@
<script setup lang="ts"></script>
<template>
<main></main>
</template>
<style scoped></style>

View file

@ -0,0 +1,7 @@
<script setup lang="ts"></script>
<template>
<main></main>
</template>
<style scoped></style>

View file

@ -0,0 +1,7 @@
<script setup lang="ts"></script>
<template>
<main></main>
</template>
<style scoped></style>