Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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