next-auth icon indicating copy to clipboard operation
next-auth copied to clipboard

Type Error: Argument of type '[Request]' is not assignable to parameter of type 'GetServerSessionParams<GetServerSessionOptions>'

Open HRNPH opened this issue 3 years ago • 13 comments

Question 💬

Did I do it wrong is this not the correct practice? (but it worked) any Idea on how to solve this? Seem like there's no example of using this method on ts either I followed This Tutorial image

Argument of type '[Request]' is not assignable to parameter of type 'GetServerSessionParams<GetServerSessionOptions>'.
  Type '[Request]' is not assignable to type '[GetServerSessionOptions]'.
    Type 'Request' has no properties in common with type 'GetServerSessionOptions'.ts(2345)

How to reproduce ☕️

Reproduce

import { getServerSession } from "next-auth";
// create a POST function with Request type in Api route
export async function POST(request: Request) {
    const session = await getServerSession(request);
    return null
}

Contributing 🙌🏽

Yes, I am willing to help answer this question in a PR

HRNPH avatar May 16 '23 15:05 HRNPH

In case of using this notation

const session = await getServerSession({req: request});

Error

Argument of type '[{ req: Request; }]' is not assignable to parameter of type 'GetServerSessionParams<GetServerSessionOptions>'.
  Type '[{ req: Request; }]' is not assignable to type '[GetServerSessionOptions]'.
    Type '{ req: Request; }' is not assignable to type 'GetServerSessionOptions'.
      Object literal may only specify known properties, and 'req' does not exist in type 'GetServerSessionOptions'.ts(2345)

HRNPH avatar May 16 '23 16:05 HRNPH

Route Handlers are not currently supported by getServerSession. See the official docs https://next-auth.js.org/configuration/nextjs#getserversession

We are working on it though, expect a solution very soon... ™️

balazsorban44 avatar May 16 '23 21:05 balazsorban44

Temporary Solution

Just for anyone facing the same issue but still! need to port to production by now

by adding ignore build errors for typescript it will still work as it does in development

next.config.js

const nextConfig = {
    typescript: {
        ignoreBuildErrors: true,
    }
}

you can skip the type error use with caution cause it will also skip the one you should've fixed it

HRNPH avatar May 31 '23 02:05 HRNPH

Why is this type not exported? How can I type functions that use getServerSession?

type GetServerSessionParams<O extends GetServerSessionOptions> =
  | [GetServerSidePropsContext["req"], GetServerSidePropsContext["res"], O]
  | [NextApiRequest, NextApiResponse, O]
  | [O]
  | []

nemanjahifolks avatar Jun 14 '23 07:06 nemanjahifolks

any update ?

johannbuscail avatar Jul 09 '23 10:07 johannbuscail

Still not working why was this closed?

Type error: Argument of type '[NextRequest, NextResponse<unknown>, any]' is not assignable to parameter of type 'GetServerSessionParams<any>'.
  Type '[NextRequest, NextResponse<unknown>, any]' is not assignable to type '[IncomingMessage & { cookies: Partial<{ [key: string]: string; }>; }, ServerResponse<IncomingMessage>, any] | [...]'.
    Type '[NextRequest, NextResponse<unknown>, any]' is not assignable to type '[IncomingMessage & { cookies: Partial<{ [key: string]: string; }>; }, ServerResponse<IncomingMessage>, any]'.
      Type at position 0 in source is not compatible with type at position 0 in target.
        Type 'NextRequest' is not assignable to type 'IncomingMessage & { cookies: Partial<{ [key: string]: string; }>; }'.
          Type 'NextRequest' is missing the following properties from type 'IncomingMessage': aborted, httpVersion, httpVersionMajor, httpVersionMinor, and 51 more.

   6 | 
   7 | async function handler(req: NextRequest, res: NextResponse) {
>  8 |   const session = await getServerSession(req, res, authOptions);
     |                                          ^
   9 | 

at least

ignoreBuildErrors: true,

will complete the build

mistercorea avatar Aug 17 '23 03:08 mistercorea

updates?

patilanz avatar Oct 28 '23 19:10 patilanz

I can confirm that this is still an issue

mxswat avatar Nov 29 '23 12:11 mxswat

@mistercorea don’t use ignoreBuildErrors, just add a // @ts-expect-error or if needed // @ts-ignore above the line that’s erroring.

FlorianWendelborn avatar Dec 10 '23 16:12 FlorianWendelborn

The linked docs don't seem to guide use for Route Handlers, although it mentions Route handlers:

When calling from the server-side i.e. in Route Handlers, React Server Components, API routes or in getServerSideProps, we recommend using this function instead of getSession to retrieve the session object.

This seems to suggest that the code presented there should work for Route Handlers, but it doesn't.

How do we get a session in Route Handlers? If we're using a DB adapter, we could query the DB directly, but it would be nice if we didn't have to.

turnerhayes avatar Jan 25 '24 17:01 turnerhayes

It is still an issue. The error: Argument of type '[NextRequest]' is not assignable to parameter of type 'GetServerSessionParams<GetServerSessionOptions>'. Type '[NextRequest]' is not assignable to type '[GetServerSessionOptions]'. Type 'NextRequest' is not assignable to type 'GetServerSessionOptions'. Types of property 'cookies' are incompatible. Type 'RequestCookies' has no properties in common with type 'Partial<CookiesOptions>'.

The Code:

// src/app/api/orders/route.ts
import { NextResponse, NextRequest } from "next/server";
import prisma from "../../../../lib/prisma"
import { getServerSession } from "next-auth";

export async function GET(req: NextRequest) {
  try {
    const session = await getServerSession(req);

    if (!session) {
      return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
    }

    const userId = session.user?.email;

    if (!userId) {
      return NextResponse.json(
        { error: "User ID not found in session" },
        { status: 400 }
      );
    }

    const orders = await prisma.post.findMany({
      where: {
        author: { email: userId },
        published: true,
      },
      include: {
        author: { select: { name: true } },
      },
    });

    return NextResponse.json({ orders });
  } catch (error) {
    console.error("Error fetching orders:", error);
    return NextResponse.json(
      { error: "Internal Server Error" },
      { status: 500 }
    );
  }
}

Jayesh-kahnani avatar Apr 29 '24 12:04 Jayesh-kahnani

facing same issue here...

Edit by maintainer bot: Comment was automatically minimized because it was considered unhelpful. (If you think this was by mistake, let us know). Please only comment if it adds context to the issue. If you want to express that you have the same problem, use the upvote 👍 on the issue description or subscribe to the issue for updates. Thanks!

kelvindesman avatar Jun 14 '24 02:06 kelvindesman

Any updates guys..?

Edit by maintainer bot: Comment was automatically minimized because it was considered unhelpful. (If you think this was by mistake, let us know). Please only comment if it adds context to the issue. If you want to express that you have the same problem, use the upvote 👍 on the issue description or subscribe to the issue for updates. Thanks!

rainnfx avatar Jul 03 '24 04:07 rainnfx