From 81de7f04381b3b4c14c7aa3c0da8fe3515712652 Mon Sep 17 00:00:00 2001 From: Abizrh Date: Sun, 10 Aug 2025 15:50:32 +0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat=20(pinia):=20register=20stores?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/plugins/persist.client.ts | 5 +++++ app/stores/user.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 app/plugins/persist.client.ts create mode 100644 app/stores/user.ts diff --git a/app/plugins/persist.client.ts b/app/plugins/persist.client.ts new file mode 100644 index 00000000..8e0b8a21 --- /dev/null +++ b/app/plugins/persist.client.ts @@ -0,0 +1,5 @@ +import piniaPluginPersistedstate from 'pinia-plugin-persistedstate' + +export default defineNuxtPlugin((nuxtApp) => { + nuxtApp.$pinia.use(piniaPluginPersistedstate) +}) diff --git a/app/stores/user.ts b/app/stores/user.ts new file mode 100644 index 00000000..84ebc90a --- /dev/null +++ b/app/stores/user.ts @@ -0,0 +1,31 @@ +export const useUserStore = defineStore( + 'user', + () => { + const user = ref(null) + + const isAuthenticated = computed(() => !!user.value) + const userRole = computed(() => user.value?.user_position_code || '') + + const login = async (userData: any) => { + user.value = userData + } + + const logout = () => { + user.value = null + } + + return { + user, + isAuthenticated, + userRole, + login, + logout, + } + }, + { + persist: { + key: 'user', + pick: ['user'], + }, + }, +)