Skip to content
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

getServerSession returns null when the route handler is invoked from a server component fetch call #7546

Closed
mkarajohn opened this issue May 14, 2023 · 1 comment
Labels
triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@mkarajohn
Copy link

mkarajohn commented May 14, 2023

Environment

  System:
    OS: Linux 5.19 Ubuntu 22.04.2 LTS 22.04.2 LTS (Jammy Jellyfish)
    CPU: (16) x64 AMD Ryzen 7 PRO 4750U with Radeon Graphics
    Memory: 1.86 GB / 14.86 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 18.12.1 - ~/.nvm/versions/node/v18.12.1/bin/node
    Yarn: 1.22.19 - /usr/bin/yarn
    npm: 8.19.2 - ~/.nvm/versions/node/v18.12.1/bin/npm
  Browsers:
    Chrome: 113.0.5672.63
    Firefox: 113.0.1
  Relevant packages:
      next: 13.4.2
      eslint-config-next: 13.4.1
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.0.4

Reproduction URL

non available

Describe the issue

Using getServerSession inside an API route handler (as per Next.js 13+) returns null when the route handler URL is invoked from within a react server component.

If the route handler URL is invoked directly from the browser address bar, getServerSession returns the correct session object

How to reproduce

Assume you have a simple route handler such as this and that you are logged in successfully using NextAuth.js

// app/api/test/route.ts
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
import { getServerSession } from 'next-auth/next';
import { NextResponse } from 'next/server';

export async function GET(request: Request) {
  const session = await getServerSession(authOptions);

  if (!session) {
    console.log('NO SESSION');
    console.log({ session });

    return new NextResponse('Unauthorized access detected', {
      status: 401,
    });
  }

  console.log('YES SESSION');
  console.log({ session });

  return NextResponse.json({status: 200})
}

If you hit http://localhost:3000/api/test from the browser address bar, you will see printed YES SESSION followed by a proper session object, in the terminal.

If you hit http://localhost:3000/api/test from a server component, such as a Page.tsx like this:

// app/page.tsx
export default async function Page() {
  const resp = await fetch(`http://localhost:3000/api/test`);
  const data = await resp.json();

  return (
   <div> ok </div>
  );
}

You will see printed NO SESSION followed by { session: null }

Expected behavior

I would expect getServerSession to work the same when it's called inside an App router route-handler function, regardless if the route-handler is invoked from within a server component or not

@mkarajohn mkarajohn added the triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. label May 14, 2023
@balazsorban44
Copy link
Member

Duplicate of #7423 (comment)

@balazsorban44 balazsorban44 marked this as a duplicate of #7423 May 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet
Development

No branches or pull requests

2 participants