How to Retrieve Session in Next.js RSC Without Getting 401 (Next.js + Hono Integration)
Hi,
I'm building a full-stack app using Next.js for the frontend and Hono.js for the backend, running on separate ports in development (localhost:3000 for Next.js and localhost:8787 for the Hono server with Better Auth).
I’m using the following logic on the client side to get the current user:
// Get the current user
getSession: cache(async () => {
const response = await api.user['me'].$get();
if (!response.ok) {
throw new Error('Failed to fetch user');
}
const result = await response.json();
return result;
});
This works perfectly inside client components.
However, when I try to use the same API call inside a Next.js RSC (Server Component), it fails with a 401 Unauthorized response.
I'd appreciate guidance on the correct way to validate or fetch the user session from within an RSC context (e.g., how to pass the cookie or handle cross-origin session verification correctly).
Thanks in advance!
1 Reply
Can you show the backend response?
You shouldn't need to handle cors if you're on the server side in nextjs since server to server communication doesn't require that as far as I know
I believe the issue is related to the cookies
Since you're talking directly to the server and the server doesn't have the user cookies then the request naturally fails
Instead of cookies based approach try jwt based one