Must the fetch method request correspond to the server side http method

30 Views Asked by At

So I'm currently creating a login/signup prompt. The signup prompt works perfectly fine and the client and server communicate just fine.

However the login prompt responds surprisingly different with a 400 of bad request if I set the method to 'post'

await fetch('http://localhost:4500/users/Login',{
            method:"POST",
            headers:{
                'Content-Type':'Application/json'
            },
            body:JSON.stringify(details)
        })

And when I set it to 'get', of course it will fail since you can't have a body property to a get method.

So with this I'm currently wondering If the fetch method has to correspond to the server http method or I'm just doing something wrong. Here's both the client and server code.

To add on, it doesn't even call the api so I can't get the log message('Got called ....')

Client:

await fetch('http://localhost:4500/users/Login',{
            method:"POST",
            headers:{
                'Content-type':'Application/json'
            },
            body:JSON.stringify(details)
        })

//Same thing as above Server:

router.post('/',passport.authenticate("local"),(request,response) => {
    console.log("Got Called in login")
    response.send(request.session.user)
})

Here's the passport strategy I'm using:

module.exports = passport.use(
    new Strategy(async (email,password,done) => {
        try{
            const userFinder = await Database.findOne({email:email})
            if(userFinder && bcrypt.compareSync(password,userFinder.password)){
                done(null,userFinder)
            }
            else throw new Error("Incorrect user Credentials")
        }
        catch(error){
            console.log(error)
            done(error,null)
        }
    })
)

Thank you for your time

0

There are 0 best solutions below