Using express-flash or connect-flash is resulting in blank messages object

I’m trying to make a NodeJS web server using express, and came across a weird bug I can’t figure out.

I am quite familiar with Express & EJS, and this time I wanted to use connect-flash (or express-flash) to display flash messages.
I read a bit about those 2 and watched a few tutorials, but cannot understand why it is not working.
The results are pretty much the same for connect-flash and express-flash, and they are as following:

I am importing express, session and then flash, initiate the view engine, the session and then the flash:

const express        = require("express");

const session        = require("express-session");
const flash          = require("express-flash");

app.set("view engine", "ejs");

app.use(session({
    secret: process.env.SESSION_SECRET,
    resave: true,
    saveUninitialized: true,
    cookie: { secure: true }
}));

app.use(flash());

I then do some authentication stuff. For example, I use passport to authenticate when a user registers:

router.post("/register", (req, res) => {
    const { name, email, password, confirmpassword } = req.body;
    let errors = [];

    if(!name || !email || !password || !confirmpassword)
        errors.push({ message: "Please fill in all fields!" });
    
    if(password !== confirmpassword) 
        errors.push({ message: "Passwords do not match!" });

    if(password.length < 6)
        errors.push({ message: "Password should be at least 6 characters long!" });

    if(errors.length > 0) {
        res.render("register", { errors, name, email });
    } else {
        User.findOne({ email: email })
        .then(user => {
            if(user) {
                errors.push({ message: "A user with this email address already exists!" });
                res.render("register", { errors, name, email });
            } else {
                const newUser = new User({
                    name,
                    email,
                    password
                });

                bcrypt.genSalt(10, (error, salt) => {
                    if(error)
                        throw error;
                    
                    bcrypt.hash(newUser.password, salt, (error, hash) => {
                        if(error)
                            throw error;
                        
                        newUser.password = hash;
                        newUser.save()
                            .then(user => {
                                req.flash("success_message", "You have successfully registered!");
                                res.redirect("/login");
                            })
                            .catch(error => console.log(error));
                    })
                })
            }
        });
    }
});

and this is the part I can’t seem to understand.
As you can see, I pass a flash message (success_message) when a new user is created. The redirect is working perfectly and in general, the whole server is working great, but the flash messages are just blank.
It happens in other instances as well, for example when logging out:

router.get("/logout", (req, res) => {
    req.logout();
    req.flash("success_message", "You logged out successfully!");
    res.redirect("/login");
});

Or when loading a page before logging in:

    ensureAuthenticated: (req, res, next) => {
        if(req.isAuthenticated())
            return next();

        req.flash("error_message", "Please log in to view that resource");
        res.redirect("/login");
    },

Or even in the passport strategy itself:

new localStrategy({ usernameField: 'email', passReqToCallback: true }, (req, email, password, done) => {
            User.findOne({ email: email })
                .then(user => {
                    if(!user)
                        return done(null, false, req.flash("error_message", "The email and password you entered did not match any of our records. Please double-check and try again."));

                    bcrypt.compare(password, user.password, (error, match) => {
                        if(error)
                            throw(error);

                        if(match)
                            return done(null, user);

                        return done(null, false, req.flash("error_message", "The email and password you entered did not match any of our records. Please double-check and try again."));
                    });
                })
                .catch(error => console.log(error));
        })
    );

I try to load the flash messages through a partial page, called messages.ejs:

<% console.log(locals) %>
<% console.log(messages) %>

<% if(messages.success_message) { %>
    <div class="alert alert-success alert-dismissible fade show" role="alert">
        <%= messages.success_message %>
        <button type="button" class="close" data-dismiss="alert" aira-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>
<% } %>


<% if(messages.error_message) { %>
    <div class="alert alert-danger alert-dismissible fade show" role="alert">
        <%= messages.error_message %>
        <button type="button" class="close" data-dismiss="alert" aira-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>
<% } %>

but the first 2 console.log simply returns the locals object, in which you can clearly see the messages are empty.

{
  settings: {
    'x-powered-by': true,
    etag: 'weak',
    'etag fn': [Function: generateETag],
    env: 'development',
    'query parser': 'extended',
    'query parser fn': [Function: parseExtendedQueryString],
    'subdomain offset': 2,
    'trust proxy': false,
    'trust proxy fn': [Function: trustNone],
    view: [Function: View],
    views: 'D:\\Workspace JavaScript\\Passager\\views',
    'jsonp callback name': 'callback',
    'view engine': 'ejs'
  },
  messages: {},
  contentFor: [Function: contentFor],
  _locals: [Object: null prototype] { messages: {} },
  cache: false
}

I don’t log the flash messages anywhere and I don’t do anything to otherwise “delete” them. I even tried passing the manually throw the router.get requests:

router.get("/login", forwardAuthenticated, (req, res) => res.render("login", {message: req.flash('success_message')}));
router.get("/register", forwardAuthenticated, (req, res) => res.render("register", {message: req.flash('success_message')}));

Yet, can’t seem to understand why they are not showing.

Does anyone have any ideas? 🙂