简体   繁体   English

NextAuth 获取 api 端点 - req.body undefined

[英]NextAuth fetching api endpoint - req.body undefined

i'm trying to use NextAuthJS for authentication in my NextJS app..我正在尝试在我的 NextJS 应用程序中使用 NextAuthJS 进行身份验证。

I'm using this following code from documentation我正在使用文档中的以下代码

  authorize: async(credentials, req)=>{
    const res = await fetch("http://localhost:3000/api/userverify",{
      method: 'POST',
      credentials: "include",
      body: JSON.stringify(credentials),
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
    })
    const user = await res.json()

    if(res.ok && user){
      return user
    }
    return null
  }

the problem is the actual fetching, in the userverify.js i have this code:问题是实际获取,在 userverify.js 中我有这段代码:

export default async function verify(res, req){
  const credentials = req.body
  console.log(credentials)
  const user = await findUser(credentials.email)
  if(user){
    if(await argon2.verify(req.body.password, user.password)){
      res.status(200).send(user)
      return user
    }
    else{
      res.status(401).send("Credentials Incorrect")
    }
  }
}

req.body in this file is undefined, im not using express only nextjs built in api routes..此文件中的 req.body 未定义,我不使用仅在 api 路由中构建的 express nextjs。

What can i do?我能做什么?

I think it is the order of arguments. did you try logging req and res .我认为这是 arguments 的顺序。您是否尝试记录reqres First argument is request and second is response :第一个参数是request ,第二个是response

export default async function verify(req, res){

Docs文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM