BA
Better Auth2mo ago
or

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!
6 Replies
The Untraceable
The Untraceable2mo ago
I check session in my server components via auth.api.getSession(headers: await headers()) I forgot where headers is from, like "next/server" maybe
or
orOP2mo ago
The problem is that in my case, the backend is running separately. I tried what you suggested, but I’m still getting a 401 error.
Vlad
Vlad2mo ago
FAQ | Better Auth
Frequently asked questions about Better Auth.
Vlad
Vlad2mo ago
No description
Vlad
Vlad2mo ago
Is what you need It fails in RSC because you don't provide user's headers
or
orOP2mo ago
tnx bro!

Did you find this page helpful?