Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript User Authentication With Express and Mongo Improving the App with Custom Middleware Writing Custom Middleware

Sean King
Sean King
5,144 Points

req.session returning undefined

I'm having a pretty difficult time using this middleware because, well... req.session is undefined when this function is imported to the file. The following does not work because req is undefined:

function loggedOut(req, res, next) {
    if (req.session && req.session.userId) {
        return res.redirect('/profile');
    } else {
        return next();
    }
}

module.exports.loggedOut = loggedOut();

...

router.post('/login', mid.loggedOut, (req, res, next) => {
    if(req.body.email &&
        req.body.password) {
        User.authenticate(req.body.email, req.body.password, function(error, user) {
            if (error || !user) {
                const err = new Error('Wrong email or password');
                err.status = 401;
                return next(err);
            } else {
                req.session.userId = user._id;
                res.redirect('/profile');
            }
        })
    } else {
        const err = new Error('Email and password required');
        err.status = 401;
        return next(err);
    }
})

The following does not return the undefined error:

router.post('/login',
                  function(req, res, next) {
                        if (req.session && req.session.userId) {
                            return res.redirect('/profile');
                        } else {
                            return next();
                        }
                    }
                        , (req, res, next) => {
    if(req.body.email &&
        req.body.password) {
        User.authenticate(req.body.email, req.body.password, function(error, user) {
            if (error || !user) {
                const err = new Error('Wrong email or password');
                err.status = 401;
                return next(err);
            } else {
                req.session.userId = user._id;
                res.redirect('/profile');
            }
        })
    } else {
        const err = new Error('Email and password required');
        err.status = 401;
        return next(err);
    }
})

but it does not work. The function is not redirecting. Any thoughts?

Sean King
Sean King
5,144 Points

I figured out the answer. I'm dumb. So here we go:

module.exports.loggedOut = loggedOut;

not

module.exports.loggedOut = loggedOut();

and I was adding the middleware to the post routes like a dummy. Problem solved!

1 Answer

Sean King
Sean King
5,144 Points

See comment below question