I'm looking for the best method to submit an authentication form using isomorphic-fetch

I've encountered an issue while trying to send an ajax request from a react/redux app to an express POST endpoint. Despite testing the server-side endpoint with Postman and confirming its correct operation, I keep receiving {message: "Missing credentials"} from passport authentication on the client side. The code snippet I'm using on the client seems fine as I have verified the data flow by logging out req.body.email and req.body.password on the server successfully through Postman. However, when I initiate this call using isomorphic-fetch with the code below, the req.body output on the server looks like this:

{ '------WebKitFormBoundaryKtn3SHumfq4YBeZ1\r\nContent-Disposition: form-data; name': '"email"\r\n\r\<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c5abb1a0b6b185a1aaa8a4acabeba6aaa8">[email protected]</a>\r\n------WebKitFormBoundaryKtn3SHumfq4YBeZ1\r\nContent-Disposition: form-data; name="password"\r\n\r\ntestPassword\r\n------WebKitFormBoundaryKtn3SHumfq4YBeZ1--\r\n' }


import fetch    from 'isomorphic-fetch'
import FormData from 'form-data'

export function signup(payload) {

    return (dispatch) => {

        let formData = new FormData()
        formData.append('email',payload.email)
        formData.append('password',payload.password)

        fetch(`${baseURL}/signup`, {
          method: 'post',
          body: formData,
          headers: new Headers({
            'content-type': 'application/x-www-form-urlencoded; charset=utf-8',
            'Accept': 'application/json, application/xml, text/plain, text/html, *.*'
          }),
        })
        .then((res) => res.json())
        .then((res) => {
            console.log('Fetch signup result:',res)
            console.log('dispatch:',dispatch)
            dispatch({
                type        : Auth_Actions.SignUp_Success,
                userObject  : res
            })
        })
        .catch((err)=>{
            console.error('Fetch signup ERROR:',err)
            dispatch({
                type        : Auth_Actions.SignUp_Fail,
                userObject  : err
            })
        });

    }
}

Answer №1

Have you attempted to include your authorization token? You could try including it in the headers section as shown below:

  headers: {
    'Accept': 'application/json',
    'Authorization': token.id,
    'Content-Type': 'application/json'
  }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When using useEffect to mimic componentWillUnmount, the updated state is not returned

My goal is to have a functional component with initialized state using useState, which can be updated through an input field. However, upon unmounting the component, I want to log the current state instead of the initial one. In this hypothetical scenario ...

What is the process for generating a file in Node and Express on my server and subsequently transmitting it to my client for download? My technology stack includes NextJS for both frontend and backend operations

How can I generate a file using Express and Node on my server, and then successfully download it to the client? My tech stack includes NextJS for both frontend and backend, and with React as the frontend framework. I'm facing difficulty figuring out h ...

The React Camera component experiences issues when trying to function on a mobile device

I've been trying to test the Camera functionality in my React application using different mobile devices like the iPad Pro and Google Pixel 6a. I experimented with both react-webcam and react-camera-pro, but unfortunately, neither seemed to work on mo ...

The state is not being successfully updated in another one of my functions

I'm completely new to the world of React and I'm a bit puzzled as to why my state isn't updating in another method of mine. Here's an example below: fetchMovies = () => { const self = this; axios.get("https://api.themoviedb. ...

Transferring information using express

Currently, my Express server is up and running and it's set to send an HTML file from the public folder of my project. The issue arises when I try to initiate a request from a client script linked in this HTML file to send data back to the server. Des ...

Using React Bootstrap, you can ensure that only a single collapse opens at a time when rendering it with a map function. This allows you to display

When a user clicks on the "View Tasks" button, I want to display the relevant tasks specific to that user. However, currently all React Bootstrap Collapse Components open up and show tasks for every user instead of just one. This issue arises because the o ...

Error: Incorrect data type found in React Route component

I've encountered an issue while attempting to utilize the Route component in my application. The error message I'm receiving reads as follows: [ts] Type '{ path: "/:shortname"; component: typeof FirstComponent; }' is not assignable ...

How can I determine the height of a React Material textfield in a different component?

Below is the code I wrote to switch between a TextField and my custom component. However, I am facing an issue in setting the height of the custom component the same as the TextField. Can anyone suggest a solution for this? <Grid item xs={12}> < ...

Is there a way to retrieve all documents based on the start and end of a specific day?

One common issue I am facing involves submitting a date to my nodejs server in a specific format

 2018-11-02T00:36:00+05:30 // The actual time should be 12:36AM However, when examining the document in my database (using studio 3T), the format appear ...

"Issue with Material-ui Autocomplete where the defaultValue does not function properly

The defaultValue was set to "Chairs" but it doesn't seem to be working as expected. Here is the code snippet: import React, { useState } from "react"; import TextField from "@mui/material/TextField"; import Autocomplete from "@mui/material/Autocomple ...

Unable to verify credentials for accessing the MongoDB database on localhost

Recently, I embarked on a new project using Express and decided to utilize MongoDB as my database due to Node.js being the backend technology. This is my first time working with Mongo, and although I can authenticate successfully from the terminal, I' ...

Is there a way to retrieve a file from Google Drive and showcase it on a webpage using the Google Drive API in NextJS?

Despite my efforts in exploring the documentation and watching various videos, I am still struggling with understanding what to do. As a beginner in Next.js and React, I am currently working on a project that involves fetching a document from Google Driv ...

Utilizing Ok & Cancel Buttons within the Material-UI DatePicker Component

Is there a way to include action buttons like "Ok" and "Cancel" below the calendar in Desktop view when using the MUI DateRangePicker? I would like to implement this feature but am unsure of how to do so. https://i.stack.imgur.com/Hg4gm.png ...

Every time Fetch() is called in Node.js, a fresh Express session is established

Here is a snippet of code from a webshop server that includes two APIs: ./login for logging in and ./products to display products. The products will only be displayed after a successful login. The server uses TypeScript with Node.js and Express, along wit ...

There was a void in the supertest request with the application/vnd content type

I'm struggling to send a request body using supertest for a post request. Despite configuring body-parser as suggested in other answers, the issue persists. I've checked solutions that mention misconfiguration of body-parser, but they seem to be ...

Calculate the total of all values associated with a dynamically generated key within an array of objects

I'm trying to calculate the sum of similar keys in an array of objects. Each object in the array will have some common keys, but not all arrays will share the same set of keys. I'm considering storing these keys in a separate array and then loopi ...

Issue with displaying InputLabel in Material-UI FormControl

https://i.stack.imgur.com/pJznX.jpg The image shows that my label is positioned behind the outline, and I'm uncertain as to why. I did not apply any specific style other than setting a minWidth property, yet it appears broken with the label hiding be ...

ReactJS encounters errors even when the backend is returning a 200 OK status code

My ReactJS frontend receives an error when sending a GET request, even though the django backend terminal shows [10/Mar/2018 23:31:08] "GET /post/ HTTP/1.1" 200 930. I am using redux sagas in my project. In src/index.js: (index.js code snippet here) In ...

The React component library we are using is encountering issues with rendering MUI Icons. An error message is popping up saying "Element type is invalid: expected a string or a class/function but got: object

After setting up a monorepo with a shared component library, I encountered an issue when trying to import components that include a MUI icon from @mui/icons-material. The error message I received was: Error: Element type is invalid: expected a string (for ...

Leveraging Framer Motion's ScrollTrigger to Securely Pin and Unpin Elements while Scrolling

I recently started using Framer Motion and I'm attempting to replicate ScrollTrigger's feature of pinning and unpinning elements while scrolling. My current setup unpins the box when the <Two /> component comes into view, but there is a sli ...