get item on localstorage from middleware
Unanswered
Gray Flycatcher posted this in #help-forum
Gray FlycatcherOP
hey i have a question i want get accessToken that stored on localstorage
from middleware.js its possible !?
from middleware.js its possible !?
16 Replies
localStorage can only be read by client-side javascript
if you want to send data to the server automatically (so that middleware can access it), you have to use cookies
@joulev if you want to send data to the server automatically (so that middleware can access it), you have to use cookies
Gray FlycatcherOP
oh i see do i need to use a package to save cookies? if i want get it i can use this one
const token = request.cookies.get("token")?.value || ""; i used cookies-next to set cookies
@Gray Flycatcher oh i see do i need to use a package to save cookies? if i want get it i can use this one
const token = request.cookies.get("token")?.value || "";
no you don't need a package. you can set the cookie on the server by using
cookies().set(), or set the cookie on the client by using document.cookie directlybut a package may make the step easier yes
Gray FlycatcherOP
i tried this one before
const token2 = cookies().set("name", "lee"); not working for me i got Invariant: cookies() expects to have requestAsyncStorage, none available.that was a bug, should be fixed now
upgrade to latest version
Gray FlycatcherOP
aaah alright thank you mate
@joulev that was a bug, should be fixed now
Gray FlycatcherOP
is that a bug ?
@Gray Flycatcher is that a bug ?
oh cookies().set() only works in route handlers
if you need to set cookies in client components then just update document.cookie directly
or use packages like next-cookies or cookies-next (i don't remember its name)
Gray FlycatcherOP
emmmm alright im just trying cookies().set() on login submit i will try it in route file thank you