0

trying to make my own snackbar and so far, it works great (kinda always has) only problem is, i've noticed using global or locals to set a variable that can be accessed on the client, is possible to catch if you are refreshing your site exactly the same time as someone is fx. logging in. So i decided to try and use req.session as im using it to store the user login.

My idea was to simply do like so:

req.session.snackbarMessage = "You're logged in!";

This idea, works fine until i need to delete the snackbarMessage. I did try:

delete req.session.snackbarMessage;

Just doesn't really work. So i figure the problem might be where i put the delete?

Here's a snippet of my profile route which is also where i wanna delete the snackbarMessage once the user is logged in:

module.exports = function (app) {

    /* GET profile page unless a session does not exist */
    app.get('/profile', function(req, res) {
        if (req.session.userInfo == undefined) {
            res.redirect('/login');
        } else {
            res.render('pages/profile', {
                user: req.session.userInfo,
                snackbarMessage: req.session.snackbarMessage
            });
            /* Clearing the message to avoid repeating */
            delete req.session.snackbarMessage;
        }
    });
};

Once the user click on the login button, the user gets redirected to /profile which is the route seen above, also just before i redirect the user, i declare the req.session.snackbarMessage which was:

req.session.snackbarMessage = "You're logged in!";

I'm really hoping someone can help me with my problem or atleast just direct me in the right direction.

Christian
  • 39
  • 1
  • 8

1 Answers1

0

Well, after many hours of speculations, i figured i might aswell just try and render a delete parameter along with it.

This is my fix to my issue:

module.exports = function (app) {

    /* GET profile page unless a session does not exist */
    app.get('/profile', function(req, res) {
        if (req.session.userInfo == undefined) {
            res.redirect('/login');
        } else {
            res.render('pages/profile', {
                user: req.session.userInfo,
                snackbarMessage: req.session.snackbarMessage,
                DeleteSnackbarMessage: delete req.session.snackbarMessage
            });
        }
    });
};

Not quite sure yet if this is a solid fix cause i haven't had the time yet to test it but im sure its a good workaround in the end.

Christian
  • 39
  • 1
  • 8