Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

131
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!