Next.js Discord

Discord Forum

Cookies.set() not showing immediately after redirect()

Unanswered
Thrianta posted this in #help-forum
Open in Discord
ThriantaOP
I have /api/auth-callback for google oauth with lucia and arctic. When I set the session cookie and redirect, my auth button on my homepage is shown as logged out, when it should be logged in. If I refresh the page the auth button flips from logged out to logged in.

98 Replies

ThriantaOP
You can see in the pictures the first request after the the /api/auth-callback cant find the cookie. but once i refresh it finds the cookie
so your issue is that this returns null?
if on first load it doesnt throw the error than its probably a issue with how the cookies() function is being used
so i would suggest just returning the cookie in the getUser function and using that for your render
ThriantaOP
that "No session" error is thrown, and then my console.log shows no cookie/user. then i refresh, and the user/cookie is found and the console.log is as expected
hm i think on rext redirect the cache is not being revalidated
try this when u return in your route handler
ThriantaOP
i tried revalidatePath("/") just before i redirect, but that didnt nothing. its probably because i have no cache to revalidate. i also tried adding unstable_noStore() to the getUser function, but it should be dynamic already (cookie()). let me try again. (im also using redirect now instead of NextResponse.redirect, as per docs)
here's a video for anyone else trying to understand
try with
revalidatePath("/", "layout");
ThriantaOP
yea i just tried that with "layout" and still the same issue
hm thats strange
maybe try one instance of cookies()
const cookieStore = cookies();
use cookieStore across your component
ThriantaOP
i dont really want to wrap getUser in unstable_cache just to add a revalidation path
@gin use cookieStore across your component
ThriantaOP
component or route handler?
@Thrianta component or route handler?
i would suggest in both
its a bad practice invoking a new instance of a function multiple times without
ThriantaOP
sure i agree. i tried it on the server, no effect
u mean in your component?
ThriantaOP
no in my route handler, where i invoked it 3 times
in my page.tsx, i only make one call to getUser, which only invokes it once anyway
your route handler works fine i guess? try in your component where the issue is
u call cookies here aswell
ThriantaOP
aaah ok
i slimmed down my home page to only call cookies once now, not even get user:

export default async function Home() {
console.log(
rendering at ${Date.now()} with cookie: ${cookies().get(AUTH_COOKIE_NAME)?.value}
)

return (
<main
same issue
u still call cokies wrong
use cookieStore const for your console.log
const cookieStore = cookies();
ThriantaOP
so like this?


export default async function Home() {
const cookieStore = cookies()

console.log(
rendering at ${Date.now()} with cookie: ${cookieStore.get(AUTH_COOKIE_NAME)?.value}
)

return (
<main cl
yes
ThriantaOP
same effect just tested
and after u reload it works?
ThriantaOP
yes
than its a issue with the cache, nextjs is not reloading the cookies cache
on redirect
thats strange
never had that issue on my apps
ThriantaOP
hmm
i can suggest something
instead of a route handler maybe try using server actions
ThriantaOP
my signout works fine
@gin instead of a route handler maybe try using server actions
ThriantaOP
i would want to, but its a redirect from google. how would i do that?
@Thrianta i would want to, but its a redirect from google. how would i do that?
u are sending the your code from the google redirect to the route handler right?
change your fetch login on the clientside to a serveraction one
otherwise i have another thing u can do
what i do sometimes
u can call a pseudo serveractions on your route handler callback to revalidate your router cache
i do that sometimes to mutate my cached serverside data
ThriantaOP
i have a login form with an action that creates state,code,and auth url, sets cookies, and redirects to auth url (google)

i complete the google steps and redirects me to /api/auth-callback.

are you suggesting i call a server action inside this route handler
u call the serveraction on the client
a pseudo action only revalidating the routercache
and i think cookies are also revalidated
ThriantaOP
where would i put that
@Thrianta where would i put that
u would put that after ur route handler callback
so dont redirect on the route handler
return something and redirect from client
ThriantaOP
mmm
u can do two things, first try only redirecting from client
if the issue still persist, u can call the pseudo action before u redirect
ThriantaOP
just hold on a second. google redirects to an api endpoint. there is no client to redirect from. are you saying i should make a callback page, that calls an action, that returns something, that i then redirect from on the client
ah i see google calls your route handler directly?
isnt there a way to get the data from google to the client?
the main tought about this is that u ->
1. call google auth
2. google returns your data to the client
3. client calls your route handler
4. handler sets httponly cookie, returns some kind of response
5. client redirects on true
ThriantaOP
yes. google passes the data as url search params
okay ill try that (in 30 min, brb) but it just feels like a sin to need to use javascript to deal with cookies
u can leave the cookiedealing on your serverside
ThriantaOP
sure. but all of this has to do with cookies is what i mean. i guess the cache is introducing the problem in the first place so js is the solution
i mean if its called like that
the page where the users opens google auth
login page
ThriantaOP
all i have is a login button component that is a form with a server action. this action ultimately redirects the page to google (essential, cant make a fetch request). now the user is on google auth screen and has to click twice. then google redirects back to my website. it currently redirects to a route handler but i think youre suggesting changing that to a page, and moving that route handler logic into a server action that the page calls.
brb
cant u catch the callback values from google auth?
if its just searchParams u can always get that on the client
ThriantaOP
wdym? the user needs to select their email address and stuff. so they need a ui. so i cant just make a fetch request. it means i must redirect them.
ThriantaOP
my temporary fix is, after setting the cookies in the route handler, to redirect to a success page, showing the user they have logged in. then from there i wait a second before redirecting them to whatever url they were on
ThriantaOP
ty for the debugging help. what ive ended up with is

- google redirecting to a client page /auth/callback
- client page captures the code/state from url and makes a fetch request to a route
- route handles validating the state and code and everything, returns 200 if it is successful
- client page handles the fetch response and pushes the user on a successful response


I dont think this solves the problem so im not gonna close this thread
ThriantaOP
It does work, but it doesnt solve the original problem of cookies not getting refreshed after the route handler updated them
i got around it using serveractions and revalidating from there
i dont set cookies that often so that was the quick fix for me
ThriantaOP
💪🏻