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: [TypeError]: Cannot read properties of undefined (reading 'cookies') #7752

Closed
mcnaveen opened this issue Jun 6, 2023 · 8 comments
Labels
incomplete Insufficient reproduction. Without more info, we won't take further actions/provide help. triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@mcnaveen
Copy link

mcnaveen commented Jun 6, 2023

Environment

 System:
    OS: Linux 5.19 Ubuntu 22.04.1 LTS 22.04.1 LTS (Jammy Jellyfish)
    CPU: (4) x64 Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz
    Memory: 5.03 GB / 15.52 GB
    Container: Yes
    Shell: 5.8.1 - /usr/bin/zsh
  Binaries:
    Node: 16.18.1 - /usr/bin/node
    Yarn: 1.22.19 - /usr/bin/yarn
    npm: 8.19.2 - /usr/bin/npm
  Browsers:
    Brave Browser: 108.1.46.144
    Chrome: 108.0.5359.124
    Firefox: 113.0.2

Describe the issue

I have this in the getServerSideProps. Basically, wants to get the session and pass it as props

But when I do this. I'm getting the below error.

image

export async function getServerSideProps({
  context,
}: {
  context: {
    req: NextApiRequest;
    res: NextApiResponse;
  };
}) {
  const session = await getServerSession(
    context?.req,
    context?.res,
    authOptions
  );

  console.log(session); // This is returning undefined

  return {
    props: {
      session: session,
    },
  };
}

How to reproduce

  • Have a basic setup of Nextauth
  • Add the above-shared code in getserversideprops

Expected behavior

It's supposed to return the session.

@mcnaveen mcnaveen added the triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. label Jun 6, 2023
@balazsorban44 balazsorban44 added the incomplete Insufficient reproduction. Without more info, we won't take further actions/provide help. label Jun 7, 2023
@github-actions
Copy link

github-actions bot commented Jun 7, 2023

We cannot recreate the issue with the provided information. Please add a reproduction in order for us to be able to investigate.

Why was this issue marked with the incomplete label?

To be able to investigate, we need access to a reproduction to identify what triggered the issue. We prefer a link to a public GitHub repository (template), but you can also use a tool like CodeSandbox or StackBlitz.

To make sure the issue is resolved as quickly as possible, please make sure that the reproduction is as minimal as possible. This means that you should remove unnecessary code, files, and dependencies that do not contribute to the issue.

Please test your reproduction against the latest version of NextAuth.js (next-auth@latest) to make sure your issue has not already been fixed.

I added a link, why was it still marked?

Ensure the link is pointing to a codebase that is accessible (e.g. not a private repository). "example.com", "n/a", "will add later", etc. are not acceptable links -- we need to see a public codebase. See the above section for accepted links.

What happens if I don't provide a sufficient minimal reproduction?

Issues with the incomplete label that receives no meaningful activity (e.g. new comments with a reproduction link) are closed after 7 days.

If your issue has not been resolved in that time and it has been closed/locked, please open a new issue with the required reproduction. (It's less likely that we check back on already closed issues.)

I did not open this issue, but it is relevant to me, what can I do to help?

Anyone experiencing the same issue is welcome to provide a minimal reproduction following the above steps. Furthermore, you can upvote the issue using the 👍 reaction on the topmost comment (please do not comment "I have the same issue" without repro steps). Then, we can sort issues by votes to prioritize.

I think my reproduction is good enough, why aren't you looking into it quicker?

We look into every NextAuth.js issue and constantly monitor open issues for new comments.

However, sometimes we might miss one or two. We apologize, and kindly ask you to refrain from tagging core maintainers, as that will usually not result in increased priority.

Upvoting issues to show your interest will help us prioritize and address them as quickly as possible. That said, every issue is important to us, and if an issue gets closed by accident, we encourage you to open a new one linking to the old issue and we will look into it.

Useful Resources

@mcnaveen
Copy link
Author

mcnaveen commented Jun 8, 2023

It was my mistake. I was trying to destruct the params and context separately.

You have to destruct the params from the context itself.

This is the right way to do it. 👇

export async function getServerSideProps(context: GetServerSidePropsContext) {
  const session = await getServerSession(
    context?.req,
    context?.res,
    authOptions
  );

  const { params } = context ?? {};
  const { id } = params ?? {};

  console.log(session); 
  console.log(id);

  return {
    props: {
      id: id,
      session: session,
    },
  };
}

@mcnaveen mcnaveen closed this as completed Jun 8, 2023
@clxrityy
Copy link

clxrityy commented Jun 9, 2023

It was my mistake. I was trying to destruct the params and context separately.

You have to destruct the params from the context itself.

This is the right way to do it. 👇

export async function getServerSideProps(context: GetServerSidePropsContext) {
  const session = await getServerSession(
    context?.req,
    context?.res,
    authOptions
  );

  const { params } = context ?? {};
  const { id } = params ?? {};

  console.log(session); 
  console.log(id);

  return {
    props: {
      id: id,
      session: session,
    },
  };
}

I'm still getting this issue:

image

@kumard3
Copy link

kumard3 commented Dec 31, 2023

It was my mistake. I was trying to destruct the params and context separately.
You have to destruct the params from the context itself.
This is the right way to do it. 👇

export async function getServerSideProps(context: GetServerSidePropsContext) {
  const session = await getServerSession(
    context?.req,
    context?.res,
    authOptions
  );

  const { params } = context ?? {};
  const { id } = params ?? {};

  console.log(session); 
  console.log(id);

  return {
    props: {
      id: id,
      session: session,
    },
  };
}

I'm still getting this issue:

image

did you find the solution?

@mcnaveen
Copy link
Author

mcnaveen commented Jan 1, 2024

It was my mistake. I was trying to destruct the params and context separately.

You have to destruct the params from the context itself.

This is the right way to do it. 👇

export async function getServerSideProps(context: GetServerSidePropsContext) {
  const session = await getServerSession(
    context?.req,
    context?.res,
    authOptions
  );

  const { params } = context ?? {};
  const { id } = params ?? {};

  console.log(session); 
  console.log(id);

  return {
    props: {
      id: id,
      session: session,
    },
  };
}

This worked for me.

@ALIRAZA17
Copy link

Guys

It was my mistake. I was trying to destruct the params and context separately.
You have to destruct the params from the context itself.
This is the right way to do it. 👇

export async function getServerSideProps(context: GetServerSidePropsContext) {
  const session = await getServerSession(
    context?.req,
    context?.res,
    authOptions
  );

  const { params } = context ?? {};
  const { id } = params ?? {};

  console.log(session); 
  console.log(id);

  return {
    props: {
      id: id,
      session: session,
    },
  };
}

I'm still getting this issue:
image

did you find the solution?

Any progress?

@ALIRAZA17
Copy link

Guys I found the Answer so you dont have to pass context.req and context.res inside getServerSession. Only pass authOptions and it will work. Worked for me!!

import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/prisma';
import { getServerSession } from 'next-auth/next';
import { authOptions } from '../[...nextauth]/authOptions';

export async function PUT(request: Request) {
  const { name, email, phone_number, username, spaceId } = await request.json();
  const session = await getServerSession(authOptions);
  console.log(session)
  try {
    const updatedUser = await prisma.user.update({
      where: { username_spaceId: { username, spaceId } },
      data: { name, email, phone_number },
    });

    return NextResponse.json(updatedUser);
  } catch (error) {
    console.error('Error updating user:', error);
    return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
  }
}

@davidawad
Copy link

The above worked for me, I was working with t3 stack, I finally switched return getServerSession(ctx.req, ctx.res, authOptions) with return getServerSession(authOptions) and it worked.

ugh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
incomplete Insufficient reproduction. Without more info, we won't take further actions/provide help. triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet
Development

No branches or pull requests

6 participants