Search code examples
javascriptreactjsfetchpassport.js

passport authentication works from postman but not with fetch()


I am using passport for my react app. When I call /users/login from postman and send login data everything works fine, when I call the following code over a button in my react app I get a bad request 400 error. So I assume something is wrong in my request transfer, thoughts?

loginUser = (loginCredentials) => {
    const url = `${baseUrl}/users/login`;
    const options = {
        method: 'POST',
        body: JSON.stringify(loginCredentials),
    };
    return fetch(url, options).then(response => {
        if (!response.ok) {
          return new Promise(resolve => resolve(null));
        }
        return response.json()
    });
}

Solution

  • I think there's some issue with headers

    add these to the request

    headers: {
        Accept: "application/json",
        "Content-Type": "application/json"
    }