Having trouble saving the server-sent cookie on the browser. Employing Axios on the client side

Here is my express code where I am utilizing express-session to manage storage and work with cookies. Since version 1.5.0, the module no longer requires cookie-parser to handle storing the cookie in req/res.

The session data is stored in a PSQL database hosted on ElephantSQL.

code snippet

The following code shows the cors configuration of my express application and how I handle req.body:

code snippet

The application is live on Railways.app:

Below is the code for making POST requests to the API in my front-end application using React and Axios:

code snippet

code snippet

The issues I'm facing include:

  • When running the app and attempting to log in, no cookie is stored in the browser. Strangely, it creates two sessions in the database. However, when I run the Node app locally and make a request to localhost from my front-end, everything works smoothly - only one session is created with the cookie being stored.

  • Based on logic, when posting a username or password that doesn't exist, it should return a 404 error but instead, it returns a 401 error which is unexpected. Again, this issue is only present when running the server locally.

  • When using Insomnia to send post requests to the deployed server via the URL, the behavior is as expected - cookies are stored correctly and only one session is created.

Answer №1

I encountered a comparable situation, and it turned out that the issue originated from the axios call inexplicably.. Have you considered utilizing the native fetch function instead of axios?

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

Tips on reversing a numeric value with scientific notation in nodeJS

Exploring the optimal method to reverse an integer (positive and negative) in NodeJS 12 without the need to convert the number to a string. This solution should also accommodate numbers written in scientific notation such as 1e+10, which represents 10000 ...

Error: Unable to use map function on users .. cannot perform mapping on functions

Initially, the map function in my code was working fine. However, suddenly an error started appearing when I included the users.map line. Surprisingly, if I comment out that line, the code works perfectly again. Even more strangely, if I uncomment it, ev ...

Encountering an issue with extending the MUI color palette, receiving a "reading 'dark'" error due to properties of undefined

Encountering an issue when trying to expand the MUI color palette—getting this error instead: Error: Cannot read properties of undefined (reading 'dark') Take a look at my theme.ts file below: const theme = createTheme({ palette: { pri ...

Learn how to easily send a collection of images to Cloudinary by utilizing Axios and React hook form within a Next.js environment

In the code snippet provided, there seems to be an issue where only a single image is uploaded to the Cloudinary API even though multiple images are specified in the input tag. The frontend logic loops through the event target files and appends them to for ...

Subscriber client successfully activated and operational via the command line interface

I have incorporated a script into my PHP file that reads the Pusher channel and performs various actions when a new event occurs on the specified channel. If I access the following URL in my browser: http:/localhost/pusher.php and keep it open, the p ...

Verifying the presence of a field in a MongoDB database using Node.js

Can someone help me with this code snippet? I'm trying to validate user input in my Node.js application... router.post('/login', function(request, response) { User.find({ 'username': request.body.email,'email':reques ...

Can you please confirm the location of the npm directory?

The Node version running is 17.7.1. Upon executing npm -v, the following error message appears: bash: /usr/local/bin/npm: No such file or directory However, running npm root -g displays the path: /usr/local/lib/node_modules Executing npm list -g results ...

Discovering the unique identifier of a React material-ui component

I've been struggling to extract the id value of a react material-ui component for quite some time now. I can't seem to understand why retrieving properties of components isn't as straightforward as it is in plain java script. Is there a spec ...

Pass float value to validation in React Number Format

Currently, I am working on a react-hook-form form with yup validation. My goal is to format an input using react-number-format and be able to pass the float value for validation as well as submission. If you want to take a look at the code, here's th ...

Server-side rendering causes issues with Redux-Observable

Check out my experimental repository on redux-observable It functions properly with webpack-dev-server, but encounters errors with server-side-rendering, displaying: TypeError: action$.ofType(...).delay is not a function Steps to replicate the issue: y ...

Unable to install serialport with npm (or 'node-gyp rebuild') when not connected to the internet

Attempting to install the serialport package (a dependency of oxygen-cli) within our internal network without internet access is proving to be a challenge. During the npm install -g serialport process, it attempts to rebuild node-gyp. After encountering a ...

Guide to transforming Tailwind CSS into a class and saving it in a CSS document

Recently, I implemented Tailwind CSS to style my application. It has been a helpful tool, but I am looking to consolidate all the CSS into one file for better organization. I want to keep it separate from the HTML code. My attempt to convert the following ...

What is the best way to test the SSM getParameter function using Jasmine?

Is there a way to effectively test this? const ssmParameterData = await ssm.getParameter(params, async (error, data) => { if (error) throw error; return data; }).promise(); I have attempted mocking the method by doing: spyOn(ssm, 'getParameter& ...

Unfolding the potential of variables in JSON.stringify with Node.js

I am currently developing a node.js API for my app, which includes a simple TCP server that accepts NDJSON (newline delimited JSON). However, I have encountered an issue with the JSON stringify method. The problem arises when I attempt to expand all variab ...

What could be causing my node.js to fail in producing a true result within the return statement?

I've encountered an issue with VS code where the return true command is not displaying anything in my terminal, while console.log(map[arr2[j]]) successfully returns true. I'm unsure if this problem lies with node or my terminal. How can I ensure ...

What are the steps to resolve the issue "Error: no valid exports main found" specifically on a Windows 7 operating system?

I've been encountering an issue while attempting to run my react app on Windows 7 OS. I have npm version 6.13.4 and node version 13.6.0 installed on my system. Every time I try to start the application using npm start, I receive the following error co ...

Leverage the router through the getServerSideProps method

My goal is to automatically redirect the user to the login page if their token has expired. I need to handle this in the getServerSideProps method where I make a request to the server for data. If the request is unauthorized, I want to use the useRouter ho ...

Ways to conceal the current state changes from being displayed in the URL

I've implemented a React form with text fields and radio buttons to store data in the state. The 'Proceed' button triggers an onClick function: handleClick(event){ console.log(this.state); var userID = 1; firebase.database().ref ...

What steps can be taken to stop the page from refreshing and losing its state when cancelling navigation using the back and forward browser buttons in React-router v4.3?

I'm currently facing an issue with a page in which user inputs are saved using a react context object. The problem arises when a user navigates away from the page without saving their entries. I want to prompt the user to either continue or cancel, wh ...

Step-by-step guide on deploying an Angular and Express project to Google App Engine

I am in the process of deploying my app to Google App Engine. I have already set up App Engine and installed gcloud on my local machine. While I have been successful in deploying some projects, I have only done so for Angular applications. My goal now is ...