diff --git a/playground/app.vue b/playground/app.vue index 05a0630..f6f0e6f 100644 --- a/playground/app.vue +++ b/playground/app.vue @@ -1,18 +1,5 @@ - - diff --git a/playground/composables/token-store.ts b/playground/composables/token-store.ts new file mode 100644 index 0000000..0a67ede --- /dev/null +++ b/playground/composables/token-store.ts @@ -0,0 +1,7 @@ +export const useTokenStore = defineStore('token', () => { + const token = ref('') + + return { token } +}, { + persist: true, +}) diff --git a/playground/middleware/login.global.ts b/playground/middleware/login.global.ts new file mode 100644 index 0000000..61e08e2 --- /dev/null +++ b/playground/middleware/login.global.ts @@ -0,0 +1,11 @@ +export default defineNuxtRouteMiddleware((to) => { + const tokenStore = useTokenStore() + + if (!tokenStore.token && to.path !== '/login') { + return navigateTo('/login') + } + + if (tokenStore.token && to.path === '/login') { + return navigateTo('/page') + } +}) diff --git a/playground/pages/index.vue b/playground/pages/index.vue new file mode 100644 index 0000000..1335e4c --- /dev/null +++ b/playground/pages/index.vue @@ -0,0 +1,44 @@ + + + + + diff --git a/playground/pages/login.vue b/playground/pages/login.vue new file mode 100644 index 0000000..2e16fb1 --- /dev/null +++ b/playground/pages/login.vue @@ -0,0 +1,17 @@ + + + diff --git a/playground/pages/page.vue b/playground/pages/page.vue new file mode 100644 index 0000000..419c311 --- /dev/null +++ b/playground/pages/page.vue @@ -0,0 +1,14 @@ + + + diff --git a/src/runtime/core.ts b/src/runtime/core.ts index 6eca056..c41d85d 100644 --- a/src/runtime/core.ts +++ b/src/runtime/core.ts @@ -73,7 +73,6 @@ function persistState( export function createPersistence( context: PiniaPluginContext, optionsParser: (p: PersistenceOptions) => Persistence, - runWithContext: (fn: () => void) => void = fn => fn(), ) { const { pinia, store, options: { persist } } = context @@ -99,21 +98,21 @@ export function createPersistence( store.$hydrate = ({ runHooks = true } = {}) => { persistences.forEach((p) => { - runWithContext(() => hydrateStore(store, p, context, runHooks)) + hydrateStore(store, p, context, runHooks) }) } store.$persist = () => { persistences.forEach((p) => { - runWithContext(() => persistState(store.$state, p)) + persistState(store.$state, p) }) } persistences.forEach((p) => { - runWithContext(() => hydrateStore(store, p, context)) + hydrateStore(store, p, context) store.$subscribe( - (_mutation, state) => runWithContext(() => persistState(state, p)), + (_mutation, state) => persistState(state, p), { detached: true }, ) }) diff --git a/src/runtime/plugin.ts b/src/runtime/plugin.ts index 361dcf0..70f06f4 100644 --- a/src/runtime/plugin.ts +++ b/src/runtime/plugin.ts @@ -1,11 +1,10 @@ import type { Pinia, PiniaPluginContext } from 'pinia' -import { defineNuxtPlugin, useNuxtApp, useRuntimeConfig } from '#app' +import { defineNuxtPlugin, useRuntimeConfig } from '#app' import { destr } from 'destr' import { createPersistence } from './core' import { storages } from './storages' function piniaPlugin(context: PiniaPluginContext) { - const nuxtApp = useNuxtApp() const config = useRuntimeConfig() const options = config.public.piniaPluginPersistedstate @@ -27,7 +26,7 @@ function piniaPlugin(context: PiniaPluginContext) { afterHydrate: p.afterHydrate, pick: p.pick, omit: p.omit, - }), nuxtApp.runWithContext) + })) } export default defineNuxtPlugin(({ $pinia }) => { diff --git a/src/runtime/storages.ts b/src/runtime/storages.ts index 6b688bf..6693327 100644 --- a/src/runtime/storages.ts +++ b/src/runtime/storages.ts @@ -1,4 +1,4 @@ -import type { PublicRuntimeConfig } from 'nuxt/schema' +import type { PublicRuntimeConfig } from '@nuxt/schema' import type { StorageLike } from '../types' import { useCookie } from '#app'