From 87a4bdc1a8a2daced6831c51e42d5f9b0345a0fb Mon Sep 17 00:00:00 2001 From: "dwi.firman" Date: Mon, 2 Jun 2025 12:08:55 +0700 Subject: [PATCH] perbaikan keycloak dan hak akses pada sidebarItem, add axios, pinia, zod --- components/auth/LoginForm.vue | 48 ++++++------ components/layout/full/Main.vue | 8 +- .../layout/full/vertical-header/ProfileDD.vue | 10 +-- layouts/default.vue | 66 ++++++++++++++++- middleware/auth.global.ts | 12 +++ nuxt.config.ts | 2 +- package.json | 17 +++-- pages/auth/Login.vue | 57 ++++++++++++--- pnpm-lock.yaml | 45 ++++++++---- server/api/auth/login.js | 73 +++++++++++++++++++ server/api/sidebarItem.ts | 19 +++++ server/utils/auth.js | 29 ++++++++ 12 files changed, 323 insertions(+), 63 deletions(-) create mode 100644 middleware/auth.global.ts create mode 100644 server/api/auth/login.js create mode 100644 server/api/sidebarItem.ts create mode 100644 server/utils/auth.js diff --git a/components/auth/LoginForm.vue b/components/auth/LoginForm.vue index 488fa7f..8f55a28 100644 --- a/components/auth/LoginForm.vue +++ b/components/auth/LoginForm.vue @@ -1,23 +1,26 @@ diff --git a/components/layout/full/Main.vue b/components/layout/full/Main.vue index 2734e1e..fdcae13 100644 --- a/components/layout/full/Main.vue +++ b/components/layout/full/Main.vue @@ -3,16 +3,18 @@ import { onMounted, ref, shallowRef, watch } from 'vue'; import { useDisplay } from "vuetify"; import sidebarItems from "@/components/layout/full/vertical-sidebar/sidebarItem"; import { Menu2Icon } from "vue-tabler-icons"; +const props = defineProps({ items: { type: Object } }) +const sidebarMenu = ref(''); -const sidebarMenu = shallowRef(sidebarItems); const { mdAndDown } = useDisplay(); const sDrawer = ref(true); onMounted(() => { sDrawer.value = !mdAndDown.value; // hide on mobile, show on desktop }); -watch(mdAndDown, (val) => { - sDrawer.value = !val; +watch([mdAndDown, () => props.items], ([mdVal, items]) => { + sDrawer.value = !mdVal; + sidebarMenu.value = items; }); diff --git a/components/layout/full/vertical-header/ProfileDD.vue b/components/layout/full/vertical-header/ProfileDD.vue index 3b63f8f..63ae659 100644 --- a/components/layout/full/vertical-header/ProfileDD.vue +++ b/components/layout/full/vertical-header/ProfileDD.vue @@ -1,13 +1,13 @@ diff --git a/layouts/default.vue b/layouts/default.vue index 233ad09..b0e02c2 100644 --- a/layouts/default.vue +++ b/layouts/default.vue @@ -12,13 +12,77 @@ useHead({ : "Matdash - Nuxt3 Typescript based Free Admin Dashboard Template"; }, }); + + +const { $encodeBase64, $decodeBase64 } = useNuxtApp(); +const sidebarMenu = ref(''); +const sessionState = ref(''); +const issuer = ref(''); +const code = ref(''); +const cobaSetProfile = ref([]); +const runtimeconfig = useRuntimeConfig(); +const url = window.location.href; +const urlParams = new URL(url).searchParams; +sessionState.value = urlParams.get('session_state') || ''; +issuer.value = urlParams.get('iss') || ''; +code.value = urlParams.get('code') || ''; + +const response = $fetch(`${issuer.value}/protocol/openid-connect/token`, { + method: 'POST', + headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, + body: new URLSearchParams({ + grant_type: 'authorization_code', + client_id: runtimeconfig.public.keycloakClient, // Ganti dengan client ID Anda + client_secret: runtimeconfig.public.keycloakSecretKey, + code: code.value, + redirect_uri: `${window.location.origin}`, // Ganti dengan redirect URI Anda + }) +}).then((response) => { + const reUserInfo = $fetch(`${issuer.value}/protocol/openid-connect/userinfo`, { + headers: { + Authorization: `Bearer ${response.access_token}`, + } + }) + .then((response) => { + const idMongose = response.idMongose + const encodeProfile = $encodeBase64(JSON.stringify(response)) + localStorage.setItem('userProfile', encodeProfile) + cobaSetProfile.value.push(encodeProfile) + $fetch(`/api/sidebarItem`, { + method: 'POST', + body: { key: idMongose } + }) + .then((response) => { + localStorage.setItem('sidebarItems', $encodeBase64(JSON.stringify(response))) + sidebarMenu.value = response.items + }).catch((err) => { + loadSidebarItemLocalStorage() + }) + // $fetch(`http://10.10.150.131:8080/api/login/${idMongose}`, { + // headers: { 'Content-Type': 'application/json', }, + // mode: 'no-cors' + // }) + // .then((response) => { + // console.log(response) + // }) + }).catch((err) => { + loadSidebarItemLocalStorage() + }) +}).catch((err) => { + loadSidebarItemLocalStorage() +}) + +function loadSidebarItemLocalStorage() { + const as = JSON.parse($decodeBase64(localStorage.getItem('sidebarItems'))) + sidebarMenu.value = as.items +}