From 32de0d6ca8b96ecf575b4dc782b45e12cd77073b Mon Sep 17 00:00:00 2001 From: Mohamed Maoulainine Maoulainine Date: Mon, 28 Apr 2025 23:54:38 +0200 Subject: [PATCH] feat: function that tells if an authentified person is an admin --- front/MyINPulse-front/src/main.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/front/MyINPulse-front/src/main.ts b/front/MyINPulse-front/src/main.ts index 063a7d7..19e6818 100644 --- a/front/MyINPulse-front/src/main.ts +++ b/front/MyINPulse-front/src/main.ts @@ -5,6 +5,7 @@ import { createPinia } from "pinia"; import piniaPluginPersistedstate from "pinia-plugin-persistedstate"; import keycloakService from "./services/keycloak"; import { type AuthStore, useAuthStore } from "@/stores/authStore.ts"; +import { jwtDecode } from "jwt-decode"; let store: AuthStore; @@ -28,6 +29,32 @@ keycloakService.CallInit(() => { } }); + + +type TokenPayload = { + realm_access?: { + roles?: string[]; + }; +}; + +function isAdmin(){ + if (store.authenticated && store.user.token) { + try { + const decoded = jwtDecode(store.user.token); + const roles = decoded.realm_access?.roles || []; + + if (roles.includes("MyINPulse-admin")) { + return true; + } else { + return false; + } + } catch (err) { + console.error("Failed to decode token", err); + } +} + +} + // this shit made by me so i can run the canva vue app //createApp(App).use(router).mount('#app');