• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Pruebas Online
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

160
Vistas
Typeof session in getServerSideProps in NextJS with next-auth

I am implementing dashboard page with credentials flow with NextJS and next-auth, I have I have augmented type of session in next-auth.d.ts as per the docs https://next-auth.js.org/getting-started/typescript#module-augmentation like so

import NextAuth from "next-auth";

declare module "next-auth" {
  interface Session extends NextAuth.DefaultSession {
    user: {
      id: string;
      email: string;
    };
  }
}

and it works as expected everywhere client side. Now I need to get the session data in getServerSide props so I follow this example https://next-auth.js.org/configuration/nextjs#unstable_getserversession and although I should get type I have set I get the oryginal DefaultSession type which is

export interface DefaultSession extends Record<string, unknown> {
    user?: {
        name?: string | null;
        email?: string | null;
        image?: string | null;
    };
    expires: ISODateString;
}

and I get errors because it tries to strigify undefineds in name and image and when I try to log session.id server side in GetServerSide props I get undefined.

This is my GetServerSideProps:

export const getServerSideProps: GetServerSideProps = async (context) => {
  const session = await unstable_getServerSession(
    context.req,
    context.res,
    authOptions
  );

  if (!session) {
    return {
      redirect: {
        destination: "/",
        permanent: false,
      },
    };
  }

  return {
    props: {}, // will be passed to the page component as props
  };
};

Thanks for any help

over 2 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda