Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
Cannot read properties of undefined (reading 'signedCookies')

I have this error with cookies in my code but I don't think it's a typo.

TypeError: Cannot read properties of undefined (reading 'signedCookies')

// auth.js 
import axios from 'axios'
axios.defaults.withCredentials = true

export const getServerSideToken = (req) => {
  const { signedCookies = {} } = req

  if (!signedCookies) {
    return {}
  } else if (!signedCookies.token) {
    return {}
  }
  return { user: signedCookies.token }
}

const WINDOW_USER_SCRIPT_VARIABLE = '__USER__'

export const getUserScript = (user) => {
  return `${WINDOW_USER_SCRIPT_VARIABLE} = ${JSON.stringify(user)}`
}

export const loginUser = async (email, password) => {
  const { data } = await axios.post('/api/login', { email, password })
  if (typeof window !== 'undefined') {
    window[WINDOW_USER_SCRIPT_VARIABLE] = data || {}
  }
}

export const getUserProfile = async () => {
  const { data } = await axios.get('/api/profile')
  return data
}

The error seems to be here and I don't know why

> 6 |   const { signedCookies = {} } = req
    |          ^
  7 | 
  8 |   if (!signedCookies) {
  9 |     return {}

I use this auth.js file in a _document.js file :

//_document.js
import Document, { Head, Main, NextScript } from 'next/document'
import { getServerSideToken, getUserScript } from '../lib/auth'

export default class MyDocument extends Document {
  static async getInitialProps(ctx) {
    const props = await Document.getInitialProps(ctx)
    const userData = await getServerSideToken(ctx.req)

    return { ...props, ...userData }
  }

  render() {
    const { user = {} } = this.props

    return (
      <html>
        <Head />
        <body>
          <Main />
          <script dangerouslySetInnerHTML={{ __html: getUserScript(user) }} />
          <NextScript />
        </body>
      </html>
    )
  }
}

Thanks in advance !

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I also got this error when I was on a tutorial video for the custom document page in next.js.

And I was able to avoid the error by using the req parameter directly in getServerSideToken like below:

export const getServerSideToken = req => {
    if (!req) {
        return {};
    } else if (!req.token) {
        return {};
    }
    return { user: req.token }
}

This leads an expected result page without error, but I'll post additional answer when I have a more fundamental solution.

I hope this is helpful to you.

about 4 years ago · Juan Pablo Isaza Denunciar
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 Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda