pinia-plugin-persistedstate icon indicating copy to clipboard operation
pinia-plugin-persistedstate copied to clipboard

[nuxt] Unable to persist if using store in middleware

Open elijah-shady opened this issue 3 years ago • 7 comments

Nuxt version: 3.0.0-rc.1 Pinia plugin persistedstate version: 1.6.1 Pinia version: 2.0.13

Hello, I reproduced your Nuxt example and it works well but if I want to use my store in a middleware, that doesn't work anymore. I have no error, just the cookie is not setted.

// /middleware/authentication.global.ts
import { defineNuxtRouteMiddleware, navigateTo, useCookie } from "#app"
import { useUser } from "~/store/user"

export default defineNuxtRouteMiddleware((to) => {
	console.log(useUser()) // this line prevents cookie creation, but reading cookie works
	if (!useCookie("user").value && to.path !== "/login") {
		return navigateTo("/login")
	}
})
// /store/user.ts
import { defineStore } from "pinia"

export const useUser = defineStore("user", {
	state: () => ({
		refreshToken: "",
		token: "",
		roles: [] as Array<string>
	}),
	getters: {
		isLogged: state => !!state.refreshToken
	},
	persist: {
		paths: ["refreshToken", "roles"]
	}
})
// /plugins/persistedstate.ts
import { createNuxtPersistedState } from "pinia-plugin-persistedstate"
import { defineNuxtPlugin, useCookie } from "#app"

export default defineNuxtPlugin((nuxtApp) => {
	nuxtApp.$pinia.use(createNuxtPersistedState(useCookie))
})

Thank you !

elijah-shady avatar Jun 14 '22 13:06 elijah-shady

Hi, i may need to look deeper into nuxt to know what is accessible from middleware and what is not 🤔

prazdevs avatar Jun 23 '22 11:06 prazdevs

Oh yes this is not accessible inside a nuxt middleware. what i did is image

stephenjason89 avatar Jul 22 '22 08:07 stephenjason89

as much as i'd like to have an impact on that issue from the plugin, it may be more of a Nuxt/cookie thing ?

prazdevs avatar Jul 23 '22 00:07 prazdevs

Yes @prazdevs , I think middleware is being called first than plugin. So nothing is being restored from storage when middleware loads.

stephenjason89 avatar Jul 23 '22 18:07 stephenjason89

i'll read some more code and undestand nuxt deeper, esp. middleware to see what we can do and if there's something the plugin should be responsible of. not sure but we'll see :)

prazdevs avatar Jul 23 '22 18:07 prazdevs

Thanks @prazdevs

stephenjason89 avatar Jul 24 '22 07:07 stephenjason89

@prazdevs I noticed that this behavior is also present inside Plugins not just middleware This makes createNuxtPersistedState useless as I can't access anything in SSR inside some plugins.

stephenjason89 avatar Aug 04 '22 12:08 stephenjason89

I was wondering if this was still an issue with the v3 and the nuxt module (@pinia-plugin-persistedstate/nuxt).

I played with middlewares a bit, and i can access and change stores values with it being always up to date and stored (in cookies by default).

Did you try it in your app/project ? Is the problem solved ?

prazdevs avatar Dec 22 '22 23:12 prazdevs

I'll be closing this issue, as it seems resolved with nuxt3 and the nuxt module. If you still have problems with it, feel free to reopen!

prazdevs avatar Dec 29 '22 10:12 prazdevs