Next.js Discord

Discord Forum

Include Cookie in Nextjs server component function

Answered
Maine Coon posted this in #help-forum
Open in Discord
Maine CoonOP
Hi all, i'm trying to send my token that is present in cookies to my api but it seems nextjs doesn't include cookies when trying to fetch from server component, this is what i'm trying to do :-
const getUserSession = async () => {
  try {
    const response = await fetch("http://localhost:3000/api/user/me") // should include token in cookie
    const data = await response.data()
    if (!response.ok) throw new Error(data.message);
    return data.data as Token
  } catch (err) {
    return null;
  }
}

const Navbar = async () => {
  const session = await getUserSession();
  console.log(session) // null
  return session ? <button>logout</button> : <button>login</button>
}

export default Navbar;

If i navigate to application, i have a valid token present in my cookies, it just doesn't work this way as mentioned above. When i debug the token in /me route handler, it is an empty string.

Can someone help?

13 Replies

American Crow
did you try to read them cookies() before your fetch?
https://nextjs.org/docs/app/api-reference/functions/cookies
@American Crow did you try to read them `cookies()` before your fetch? https://nextjs.org/docs/app/api-reference/functions/cookies
Maine CoonOP
yes i tried to do below but it throws an error saying: You're importing a component that needs next/headers. That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/getting-started
import { cookies } from "next/headers";
const getUserSession = async () => {
  try {
    const response = await fetch("http://localhost:3000/api/user/me", {
      headers: { Cookie: cookies().toString() }
    }) // should include token in cookie
    const data = await response.data()
    if (!response.ok) throw new Error(data.message);
    return data.data as Token
  } catch (err) {
    return null;
  }
}
American Crow
Had to search it. I knew i read something about it. This might give some backround info:
https://nextjs-faq.com/fetch-api-in-rsc
@American Crow Had to search it. I knew i read something about it. This might give some backround info: https://nextjs-faq.com/fetch-api-in-rsc
Maine CoonOP
actually, i need to get my logged in server session, so in order for that i need to hit the api /me which takes my token present in cookie and gets the jwt token, verify it and decodes it... then get the user details from db...
Maine CoonOP
how does getServerSession in next-auth works? i want to do something similar but using an api endpoint... what could be the best approach?
how do i maintain logged in session on server side... in client its easy, i created a context provider... but in server side i'm kinda confused...
Britannia Petite
use server actions
Answer
American Crow
I read a bit more about it ( i have no environment to test though).
Does this help you?
https://jonmeyers.io/blog/forwarding-cookies-from-server-components-to-route-handlers-with-next-js-app-router/
Britannia Petite
mark your topic as solved with my answer ! enjoy 🙂