How about I visit the campgrounds/edit page for a change?

In this get route, the previous link it was redirected from is stored in the req.session object under returnTo. Once redirected, it goes to the /login and we are able to view the object in the console.

router.get('/login', (req, res) => {
    console.log('get in user route..', req.session)
    res.render('users/login')
})
req.session :

https://i.stack.imgur.com/xPIgB.png

router.post('/login', passport.authenticate('local', { failureFlash: true, failureRedirect: '/login' }), (req, res) => {
    req.flash('success', 'Welcome back!')
    const redirectUrl = req.session.returnTo || '/campgrounds'
    res.redirect(redirectUrl)
})

However, upon submitting the login details, it consistently redirects to the /campgrounds route.

Answer №1

Upon reviewing the latest version of passport.js, I came across a change where the session id is now updated, resulting in me losing access to it. To work around this issue, I created a variable

let ran = ''

and saved res.locals.returnTo in the ran variable.

res.locals.returnTo = req.session.returnTo
    ran = req.session.returnTo
    res.render('users/login')

This allows us to still retrieve the return to url even with the updated session id.

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

Leveraging AJAX and jQuery for data storage

Need a solution using AJAX and jQuery to save data from one form in another form while preserving the values. The stored information should be hidden from the front end user, with an option for the user to delete the data if desired. Retrieving the stored ...

The additional cost associated with using a React hook is called the "

Our system includes a theme context provider that passes down a theme to all child components, calculated based on the device's dimensions. We can easily access these values using the useTheme hook in any component. In addition, we have a constants f ...

Increase the Z-Index of the Header above the Material UI Modal Background

Currently, I'm in the process of developing a mobile version of a web application using Material UI. However, I am encountering some challenges when it comes to matching the designs accurately. My approach involves utilizing the MUI App-Bar in conjunc ...

Hide Address with Jquery Dialog Box

How can I securely redirect a user to another page within the same website? In my specific situation, I have a form where users input information. For cancellations, they should have options available. When a user clicks the cancellation button, a dialog ...

Utilizing Node and Socket.io to transmit data periodically via websockets from a CSV file

I am relatively new to working with Node.js and Express.js. My goal is to set up a websocket server that can send CSV data at irregular intervals stored within the file itself, line by line. The structure of the CSV looks something like this: [timeout [ms] ...

Transfer information from .gs (Google Apps Script) to a variable inside a <script> tag in an HTML document

[ {"id":1,"label":"Node 2"}, {"id":2,"label":"Node 3"}, {"id":3,"label":"Node 4"}, {"id":4,"label":"Node 5"} ] Hello there! The function getArray() in the code snippet above is returning the specified string ↑. I need help connecting this data w ...

Combine es6 imports from the identical module using an Eslint rule or plugin

Looking to consolidate my ES6 imports from a single module into one for my React project. For example: import { Title } from "@mantine/core"; import { Center } from "@mantine/core"; import { Divider } from "@mantine/core"; T ...

What steps are needed to resolve an "undeclared identifier" error when using a NanAsyncWorker parameter?

I recently came across this code snippet from a GitHub link at https://github.com/nodejs/nan/tree/master/examples/async_pi_estimate and it sparked my curiosity. class PiWorker : public NanAsyncWorker { public: PiWorker( NanCallback *callback, NanUtf8S ...

Ways to append a number to the start or end of an array using JavaScript

Can you help me figure out why the function to order the "s" array from minor to major is adding the number at the end instead of the beginning? The if statement seems to be true (2 < 7 = true) but the logic is not working as expected. Any advice on how to ...

Sentry's Trail Track feature in AWS Lambda

Upon freezing and restarting the AWS Lambda function (hot start), we have noticed that the previous breadcrumb messages persist, appearing on the Sentry dashboard as old messages. It appears that the breadcrumbs are not cleared after the captureException ...

What is the best way to transfer data from a parent component to a child component in ReactJs?

When dealing with nested react elements, how can you pass values from the parent element to a child element that is not directly inside the parent element? React.render( <MainLayout> <IndexDashboard /> </MainLayout>, document.b ...

Sending numerous files from a Google Cloud virtual machine to Google Cloud Storage through node.js and a library called Glob

Exploring the use of Node.js for uploading multiple files from my Google Compute Engine VM local directory to a pre-existing GCS bucket has hit a snag. With each script execution, I encounter the following error message: TypeError [ERR_INVALID_ARG_TYPE]: ...

Assistance requested with Javascript for an HTML dropdown menu

My question involves utilizing two HTML drop down menus. <select> <option value="">Please Select</option> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option ...

Using node.js with express to connect and link CSS and JS files

I am facing an issue with linking my CSS/JS files on a specific page. The linking works fine on localhost:5000/books/, but does not work on localhost:5000/books/1. app.use(express.static(__dirname + "/public")); bookRouter.route('/') ...

Is the automatic garbage collection of components a built-in feature of

Consider this scenario, where I have the following HTML document: <html> <head>... including all the necessary js and css files here...</head> <body> <div class="placeholderForExtPanel"></div> </b ...

can a computed property be delayed in its calculation?

Within the code snippet below, we can see that in the compPropsIsBtnDigitizePolygonDisabled function, it initially checks if the digitizePolygonInteractions variable is initialized. If it is not initialized, an error will be triggered. During execution, w ...

Import the information into a td tag's data-label property

I have implemented a responsive table design that collapses for smaller screens and displays the table header before each cell. body { font-family: "Open Sans", sans-serif; line-height: 1.25; } table { border: 1px solid #ccc; border-collapse: co ...

"Encountered an error: User.findAll function cannot be found

Here is the content of my user.js file: var sequelize = require('sequelize'); var bcrypt = require('bcrypt'); module.exports = function(sequelize, DataTypes) { const User = sequelize.define('users', { user_id: { ...

Slideshow feature stops working after one cycle

Hey there! I'm currently working on a function that allows for sliding through a series of images contained within a div. The goal is to cycle back to the beginning when reaching the end, and vice versa when going in the opposite direction. While my c ...

What is the best way to send a POST request with parameters to a PHP backend using AJAX?

This is an updated version of a previous question that was identified as a duplicate (How can I send an AJAX POST request to PHP with parameters?) I am currently sending an AJAX request to a PHP backend. Here are the JavaScript functions used to make and ...