How to access cookies in the Next.js API

Asked 1 years ago, Updated 1 years ago, 302 views

Hello, I'm using Nexst.js I'm a junior front-end developer.

To access the DB API I also configured API for next Failed to get the token value stored in the cookie I'd appreciate it if you could tell me how

The file below is Present in src/pages/api

const getOrder = async (token, order_no) => {
  return await 
   fetch(`${process.env.NEXT_PUBLIC_API_HOST}/front/order/${order_no}`, {
    headers: {
      Authorization: `Bearer ${token}`,
      Accept: "application/json"
    }
  }).then(res => res.json()).then(data => data)
}

export default async function handler(req, res) {
  const { order_no } = req.query
  const token = "How do I access cookies????"

  const order = await getOrder(token, order_no)
     try {
       res.status(200).json({ ...order })
     } } catch (err) {
       res.status(200).json({ err })
     }
   }

nextjs cookie

2023-05-28 13:30

1 Answers

req.cookies를 이용하세요

In P.S. JavaScript, the variable name is camelCase such as orderNo. snake_case such as order_no is used in languages such as Python, so we recommend you to change it.

Of course, if that's the case with the convention set by the place you belong, you should do it, and it's a personal project, so I don't mind using it if you really want to, but I told you the general case.


2023-05-29 23:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.