Search code examples
typescriptnext.jsnext-auth

next-auth type error with getServerSession


I'm getting a type error when calling the getServerSesssion function and passing all the required parameters. I have a function getServerAuthSession:

import { authOptions } from '@/pages/api/auth/[...nextauth]';
import type { GetServerSidePropsContext } from 'next';
import { getServerSession } from 'next-auth/next';

export const getServerAuthSession = (ctx: {
  req: GetServerSidePropsContext['req'];
  res: GetServerSidePropsContext['res'];
}) => {
  return getServerSession(ctx.req, ctx.res, authOptions);
};

But I get the following type error:

Argument of type '[IncomingMessage & { cookies: Partial<{ [key: string]: string; }>; }, ServerResponse<IncomingMessage>, { adapter: Adapter; providers: OAuthConfig<...>[]; secret: string | undefined; cookies: { ...; }; callbacks: {}; }]' is not assignable to parameter of type 'GetServerSessionParams<GetServerSessionOptions>'.
  Type '[IncomingMessage & { cookies: Partial<{ [key: string]: string; }>; }, ServerResponse<IncomingMessage>, { adapter: Adapter; providers: OAuthConfig<...>[]; secret: string | undefined; cookies: { ...; }; callbacks: {}; }]' is not assignable to type '[IncomingMessage & { cookies: Partial<{ [key: string]: string; }>; }, ServerResponse<IncomingMessage>, GetServerSessionOptions]'.
    Type at position 2 in source is not compatible with type at position 2 in target.
      Type '{ adapter: Adapter; providers: OAuthConfig<GoogleProfile>[]; secret: string | undefined; cookies: { sessionToken: { name: string; options: { httpOnly: boolean; sameSite: string; path: string; domain: string; secure: boolean; }; }; }; callbacks: {}; }' is not assignable to type 'GetServerSessionOptions'.
        Type '{ adapter: Adapter; providers: OAuthConfig<GoogleProfile>[]; secret: string | undefined; cookies: { sessionToken: { name: string; options: { httpOnly: boolean; sameSite: string; path: string; domain: string; secure: boolean; }; }; }; callbacks: {}; }' is not assignable to type 'Partial<Omit<AuthOptions, "callbacks">>'.
          Types of property 'adapter' are incompatible.
            Type 'Adapter' is not assignable to type 'Adapter<boolean> | undefined'.
              Type 'Adapter' is not assignable to type 'DefaultAdapter & { createVerificationToken: (verificationToken: VerificationToken) => Awaitable<VerificationToken | null | undefined>; useVerificationToken: (params: { ...; }) => Awaitable<...>; }'.
                Type 'Adapter' is not assignable to type 'DefaultAdapter'.
                  Types of property 'createUser' are incompatible.
                    Type '((user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>) | undefined' is not assignable to type '(user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>'.
                      Type 'undefined' is not assignable to type '(user: Omit<AdapterUser, "id">) => Awaitable<AdapterUser>'.ts(2345)

I've followed the config as per the documentation here: https://next-auth.js.org/configuration/nextjs

Any pointers would greatly be appreciated.


Solution

  • From the error, in your [...nextauth].ts file, you are using an adapter of type Adapter so authOptions passed as 3rd parameter to getServerSession, is not assignable to what should be there, the error tells that type Adapter is not assignable to type DefaultAdapter.

    in this section there is an example of using adapters with TypeScript in next-auth.
    they import Adapter from "next-auth/adapters" but I think you should use an adapter of type DefaultAdapter in your authOptions object so in [...nextauth].ts file.

    import { DefaultAdapter } from "next-auth/adapters";