-
Notifications
You must be signed in to change notification settings - Fork 246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auth data is not present on serverside - NextJS App Router #1528
Comments
Hello @SimonHylander thank you for reporting this. |
Hi! I have noticed the same behavior. When I authenticate using a magic link, I get redirected to my page with |
Hi y'all! Using the provided reproduction I'm not able to see the issue. Immediately after login, I'm getting I'm going to close this, but if anyone is able provide clear reproduction steps we'd be happy to take another look. |
I also encounter this. |
I have similar issue |
i experience the same issue but after some debugging it seems to only affect Firefox. In Firefox i got the following warning in the console EDIT: this seems to only affect dev environment on localhost |
I have same problem in production environment. Localhost always works fine. I am using following lines to check auth in API service and userId is always null on server side. My app also print user info in another client pages and there is OK.
|
I'm also encountering this bug. Similar to @clementprevot, immediately after login I get a null auth state on the server-side but a non-null auth state on the client side. This prevents redirecting to a "post-login" SSR page that performs a few required account-update activities before redirecting the user to their eventual destination For me this problem:
|
I'm afraid I'm also seeing the same problem. Client side authenticates fine, server returns What's weird is that it's working on |
Okay, I did some more debugging and managed to make it work by deleting the For whatever reason, this cookie wasn't being cleared or replaced correctly when I logged out. Its value was stuck at I'm still not sure why:
On (2), my suspicion is that it was triggering an invalid JWT handshake, which would explain why I was seeing a |
Same issue with Firefox. @BRKalow could you reopen the issue? I believe it's reproducible with Firefox, not Chrome. |
I had the same issue when I was trying to fetch a POST request from a Server Component to a Route Handler. I could get the auth data in the server component using auth() or currentUser(), but it showed null on the route handler. Turns out doing this is a mistake and you should not be fetching route handlers from a server component as it says in this video by Vercel. I guess is not a 'fix' but after changing my logic to the server component I can say the issue is fixed now. |
i was having this same issue on chrome where where my middleware function kept redirecting to the sign-in even though i was authenticated in clerk. also debug mode showed that all auth fields were null. this was the case only in production and didnt happen while i was on localhost. i opened the prod deployment in chrome incognito mode and suprisingly this error was gone and everything worked fine. I stopped being redirected to sign-in even though im logged in. this hinted that the problem was cookies to do with cookies. So, i cleared my cookies for my production domain and then everything worked as originally intended. tldr; clearing cookies fixed this same issue for me. |
Clearing the cookies in local development cleared the issue of seeing null returned for auth() and currentUser() in a server component. |
Hmm, tried to clear cookies and same error results. Both in FFOX and chrome. @clerk/[email protected] |
I had the same error here's how I fixed it: OS: Mac How I got this error:
How I fixed: |
Hi ! I am also having the same issue on my local development environment. It works fine on the client side components and return null in serverside components. I've tried clearing cookies and cache data but the problem still exists. If somebody found any solutions to this issue please reply? Thank you! |
why would you close this issue if its not solved?!! |
Just to add to the discussion, I turned on my sync with server for time/data, cleared cookies, tried with VPN on and off, set firefox to accept cookies from session locally (0.0.0.0:3000 localhost:3000), still get the cookie “__client_uat” = 0 error. Ubuntu 24.04, @clerk/[email protected], @clerk/[email protected], node v20.11.0, [email protected] |
Same issue, can't access auth() within an api route in next js. |
Same issue here. Tried to clear cookies and switch browsers, but still have the problem. |
Same issue here. Works fine in localhost. But when hosted on vercel or any other hosting platform, the auth is not present on serverside (and user gets redirected to sign in page). But in clientside, I can see the signed in user with @GGalupo , @rlking1985 , are you facing this issue in localhost? or only in hosted version? |
I spent the whole day tryinna fixing it, it looks like if you are using route handler in app route. app/api/route.ts currentUser or auth() always returns back null. Instead, you can use server actions directly in Nextjs which seems to be working fine. Otherwise, you can still define and pass next in your middleware/ header and cookie. |
It works when getting userId out of auth() from CSC & SSC, as well as making requests from the client side to the API route handler where I use the auth function to get userId. I get the nullish value of userId, only when I run the fetch function from the SSC to the API route handler. I guess that's maybe because SSC loses the context of the clerk auth when making data fetching. So make sure making the request from client component. |
I'm having the same issue, I'm using NextJs 13 but using Pages, not the app directory, I'm also using tRpc, on the client side I'm logged in and can see the user, but in the server side, I'd like to know if there's plans for this to be fixed or any work around? we are planning to migrate the authentication in our website, but with this bug would be impossible to recommend Clerk, can't even test it |
I found out that it's not only Clerk, Next-auth also does the same, please sure you are not sending the requet from SSC to api route directly, it should be from client component to api route, then it works. For just trying you can create a GET function in api/test/route.ts and get the user using auth(), it should work with GET in api route. |
@nandoviski I don't think that |
@ozair-dev sorry, I mean |
@elnfar thanks, I tried but didn't work, I login in the website, and called the endpoint, but it returns null, maybe I'm doing something wrong, but I doing everything the say in the docs |
Just, are you sure you are passing data back to server after the user is singed in or up? You can also log your request to see what to do you have there, and make sure you have clean config in middleware.ts for Clerk ( it changed after the update ). |
@elnfar the middleware is what I got from the docs, |
There doesn't seem to be a definitive explanation as to why this is happening. Why does this issue arise only in production while the exact same code works in dev? |
Package + Version
@clerk/clerk-js
@clerk/clerk-react
@clerk/nextjs
@clerk/remix
@clerk/types
@clerk/themes
@clerk/localizations
@clerk/clerk-expo
@clerk/backend
@clerk/clerk-sdk-node
@clerk/shared
@clerk/fastify
@clerk/chrome-extension
gatsby-plugin-clerk
build/tooling/chore
Dependencies + versions
Browser/OS
node 18.16.1
Description
After logging in, all auth properties are null in the server component using the auth() function.
Auth properties are present using a client component with useAuth().
If I try to log auth in the "afterAuth" middleware all properties are also null.
only works in client components.
demo project https://github.com/SimonHylander/next-app-router-clerk
The text was updated successfully, but these errors were encountered: