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'], + }, + }, +)