feat: logged-in check toegevoegd in de router
This commit is contained in:
		
							parent
							
								
									4f67b1e9fe
								
							
						
					
					
						commit
						0d15d068cd
					
				
					 2 changed files with 29 additions and 13 deletions
				
			
		|  | @ -1,26 +1,24 @@ | ||||||
| <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 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) | ||||||
|     //TODO: use localStorage to know which one in logged in, teacher or student |     const path = "/user"; | ||||||
|     const isTeacher = route.path.includes("teacher"); |  | ||||||
| 
 | 
 | ||||||
|     const userId = route.params.id as string; |     const role = localStorage.getItem("activeRole"); | ||||||
| 
 |  | ||||||
|     const role = isTeacher ? "teacher" : "student"; |  | ||||||
|     const name = "Kurt Cobain"; |     const name = "Kurt Cobain"; | ||||||
|     const initials = name |     const initials = name | ||||||
|         .split(" ") |         .split(" ") | ||||||
|         .map((n) => n[0]) |         .map((n) => 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" }, | ||||||
|  | @ -41,7 +39,7 @@ | ||||||
|                 <ul> |                 <ul> | ||||||
|                     <li> |                     <li> | ||||||
|                         <router-link |                         <router-link | ||||||
|                             :to="`/${role}/${userId}`" |                             :to="`${path}`" | ||||||
|                             class="dwengo_home" |                             class="dwengo_home" | ||||||
|                         > |                         > | ||||||
|                             <img |                             <img | ||||||
|  | @ -55,7 +53,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") }} | ||||||
|  | @ -63,14 +61,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> | ||||||
|  |  | ||||||
|  | @ -12,6 +12,7 @@ import CallbackPage from "@/views/CallbackPage.vue"; | ||||||
| import UserDiscussions from "@/views/discussions/UserDiscussions.vue"; | import UserDiscussions from "@/views/discussions/UserDiscussions.vue"; | ||||||
| import UserClasses from "@/views/classes/UserClasses.vue"; | import UserClasses from "@/views/classes/UserClasses.vue"; | ||||||
| import UserAssignments from "@/views/classes/UserAssignments.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), | ||||||
|  | @ -20,15 +21,18 @@ 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} | ||||||
|         }, |         }, | ||||||
| 
 | 
 | ||||||
|         { |         { | ||||||
|  | @ -99,9 +103,23 @@ const router = createRouter({ | ||||||
|             path: "/:catchAll(.*)", |             path: "/:catchAll(.*)", | ||||||
|             name: "NotFound", |             name: "NotFound", | ||||||
|             component: NotFound, |             component: NotFound, | ||||||
|             meta: {requiresAuth: true}, |             meta: {requiresAuth: false} | ||||||
|         }, |         }, | ||||||
|     ], |     ], | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
|  | router.beforeEach(async (to, from, next) => { | ||||||
|  | 
 | ||||||
|  |     // Verify if user is logged in for paths that require access
 | ||||||
|  |     if (to.meta.requiresAuth) { | ||||||
|  |         if (!authState.isLoggedIn) { | ||||||
|  |             next("/login"); | ||||||
|  |         } else { | ||||||
|  |             next(); | ||||||
|  |         } | ||||||
|  |     } else { | ||||||
|  |         next(); | ||||||
|  |     } | ||||||
|  | }); | ||||||
|  | 
 | ||||||
| export default router; | export default router; | ||||||
|  |  | ||||||
		Reference in a new issue
	
	 Joyelle Ndagijimana
						Joyelle Ndagijimana