NextJs App Router - JWT Token Authorization Issue for Server Side Components

21 Views Asked by At

This is my first time dealing with Next.Js (previously had experience with React). I had a question regarding JWT/authorization through server side rendered components.

If I have a JWT token in an HTTP-only cookie, how do I pass the cookie in Server-Side rendered components to successfully authorize the user to use my backend endpoint in the Python/Flask server? In the client side, as long as I pass the option {with credientials=true}, it successfully bypasses the @jwt_required decorator.

However, for server side components, I read that you have to pass it explicitly, but it still does not work. If anyone has any advice on this issue, please let me know.

What I tried so far was extracting the cookie from the header using next/headers package, but it still returns a 401 error

Thank you in advance for your help.

async function getAllIntegrations() {
  const cookieStore = cookies();
  const jwt_token = cookieStore.get('jwt_token')?.value;
  try {
    const response = await axios.get(`http://127.0.0.1:8000/integrations/`, {
      withCredentials: true,
      headers: {
        Authorization: `Bearer ${jwt_token}`,
      },
    });
    return response.data;
  } catch {
    throw new Error('Internal Server Error');
  }
}
0

There are 0 best solutions below