Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

135
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda