简体   繁体   中英

NextAuth fetching api endpoint - req.body undefined

i'm trying to use NextAuthJS for authentication in my NextJS app..

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:

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..

What can i do?

I think it is the order of arguments. did you try logging req and res . First argument is request and second is response :

export default async function verify(req, res){

Docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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