* fix: changed test text to use should * fix: corrected typo Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * fix: corrected typo Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * fix: remove unnecessary backslash Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * fix: simplified text Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * fix: added period Co-Authored-By: Manish Giri <manish.giri.me@gmail.com>
3.0 KiB
3.0 KiB
id, title, challengeType, forumTopicId
id | title | challengeType | forumTopicId |
---|---|---|---|
5895f70df9fc0f352b528e6a | Create New Middleware | 2 | 301551 |
Description
ensureAuthenticated(req, res, next)
, which will check if a user is authenticated by calling passports isAuthenticated on the request which in turn checks for req.user is to be defined. If it is then next() should be called, otherwise we can just respond to the request with a redirect to our homepage to login. An implementation of this middleware is:
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/');
};
Now add ensureAuthenticated as a middleware to the request for the profile page before the argument to the get request containing the function that renders the page.
app
.route('/profile')
.get(ensureAuthenticated, (req,res) => {
res.render(process.cwd() + '/views/pug/profile');
});
Submit your page when you think you've got it right.
Instructions
Tests
tests:
- text: Middleware ensureAuthenticated should be implemented and on our /profile route.
testString: getUserInput => $.get(getUserInput('url')+ '/_api/server.js') .then(data => { assert.match(data, /ensureAuthenticated[^]*req.isAuthenticated/gi, 'Your ensureAuthenticated middleware should be defined and utilize the req.isAuthenticated function'); assert.match(data, /profile[^]*get[^]*ensureAuthenticated/gi, 'Your ensureAuthenticated middleware should be attached to the /profile route'); }, xhr => { throw new Error(xhr.statusText); })
- text: A Get request to /profile should correctly redirect to / since we are not authenticated.
testString: getUserInput => $.get(getUserInput('url')+ '/profile') .then(data => { assert.match(data, /Home page/gi, 'An attempt to go to the profile at this point should redirect to the homepage since we are not logged in'); }, xhr => { throw new Error(xhr.statusText); })
Challenge Seed
Solution
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/