OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

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

  • Thread starter Thread starter Boban Banjevic
  • Start date Start date
B

Boban Banjevic

Guest
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

Code:
<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

Code:
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.

<p>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 '/'.</p>
<p>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.</p>
<p>This is my script code in products.ejs</p>
<pre><code><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>
</code></pre>
<p>And this is my index.js login post</p>
<pre><code>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();
}
});
</code></pre>
<p>I tried all the thing mentioned above, and I even tried doing <code>sessionStorage.getItem('redirectUrl')</code> and I definitely get the item value, that is the path.</p>
 

Latest posts

Top