Session storage key value returns undefined even though it’s set

Inside my products.ejs file in script I set key value to the current path. In my main indexjs file, in POST method to /login I try to access that path and use it to redirect to it after successful log in. In case there is no such a path, just use default ‘/’.

But using request.body.redirectUrl, request.session.redirectUrl i still get default value, and when I try to remove default path, I get /undefined. I can’t get that value and I am out of ideas.

This is my script code in products.ejs

<script>
    document.addEventListener('DOMContentLoaded', function() {
        const addToCartButtons = document.querySelectorAll('.add-to-cart');

        addToCartButtons.forEach(button => {
            button.addEventListener('click', function(event) {
                const loggedInData = document.getElementById('loggedInData').getAttribute('data-loggedin');

                if (loggedInData === 'false') {
                    event.preventDefault();   // Prevent the default action of the button click
                    const currentPageUrl = window.location.href;
                    // Save current URL to session storage
                    sessionStorage.setItem('redirectUrl', currentPageUrl);
                    let a = sessionStorage.getItem('redirectUrl')
                    window.location.href = `/login`;
                } else {
                    // Add your add to cart functionality here
                    console.log('Adding to cart...');
                }
            });
        });
    });

</script>

And this is my index.js login post

app.post('/login', function (request, response) {

    let username = request.body.username;
    let password = request.body.password;

    if (username && password) {
        // Execute SQL query that'll select the account from the database based on the specified username and password
        connection.query("SELECT * FROM users WHERE users_username = ? AND users_password = ? AND users_status = 'active'", [username, password],
            function (error, results, fields) {
                // If there is an issue with the query, output the error
                if (error) throw error;
                // If the account exists
                if (results.length > 0) {
                    // Authenticate the user
                    request.session.loggedin = true;
                    request.session.username = username;
                    request.session.id = results[0].id;
                    // Retrieve redirectUrl from sessionStorage
                    let redirectUrl = request.body.redirectUrl; // Default to '/' if redirectUrl is not set
                    response.redirect(redirectUrl);
                } else {
                    response.render('login', {
                        message: 'Incorrect Username and/or Password!',
                        error: {status: "", stack: ""}
                    });
                    // response.send('Incorrect Username and/or Password!');
                }
                response.end();
            });
    } else {
        response.render('login', {
            message: 'Please enter Username!',
            error: {status: "", stack: ""}
        });
        response.end();
    }
});

I tried all the thing mentioned above, and I even tried doing sessionStorage.getItem('redirectUrl') and I definitely get the item value, that is the path.