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

437
Views
next.js iron-session req.session.user is undefined

I'm making a dashboard for my team's bot using next.js and iron-session.

However, when I save the session and check it, req.session.user is undefined.

How can I fix this error???

Here is my code with the error:

page/auth.js:

Auth.getInitialProps = async ({ query }) => {
  //console.log(query.code)
  const result = await axios.get(`https://lunasite.prnm789.repl.co/api/sign/in?code=${query.code}`)
  const { access_token, refresh_token } = await result.data
  const newsession = await axios.post(`https://lunasite.prnm789.repl.co/api/session/new`, {
    access_token: access_token,
    refresh_token: refresh_token
  })
  ...
}

page/api/session/new.js:

import { withIronSessionApiRoute } from 'iron-session/next'
import { sessionOptions } from '../../../lib/config'

export default withIronSessionApiRoute(userRoute, sessionOptions)

async function userRoute(req, res) {
  if (req.method == "POST") {
    const { access_token, refresh_token } = req.body
    ...

    try {
      req.session.user = {
        isLoggedIn: true,
        access_token: access_token,
        refresh_token: refresh_token
      }
      await req.session.save()
      ...
    } catch(e) {
      ...
    }
  } else {
    ...
  }
}

lib/config.js:

export const sessionOptions = {
  cookieName: "luna_cookie",
  password: process.env.sessionCookiePass,
  cookieOptions: {
    secure: process.env.NODE_ENV === "production",
    maxAge: 604800 - 60
  },
}

page/api/session/get.js:

import { withIronSessionApiRoute } from "iron-session/next";
import { sessionOptions } from "../../../lib/config";

export default withIronSessionApiRoute(getRoute, sessionOptions);

function getRoute(req, res) {
  res.send(String(req.session.user))
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Can you check if your front-end domain and network request url are the same for me? I had this problem which I set baseurl for my request from NEXT_PUBLIC_VERCEL_URL and it was an autogenerated url for that unique deployment, long story short my session was saved on main domain but my requests were on something else. as soon as I replaced it with an absolute path all my problems went away

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!