Skip to content

Instantly share code, notes, and snippets.

@andresmatasuarez
Last active August 29, 2015 14:11
Show Gist options
  • Save andresmatasuarez/af2c02ee0b35650faa63 to your computer and use it in GitHub Desktop.
Save andresmatasuarez/af2c02ee0b35650faa63 to your computer and use it in GitHub Desktop.
Passport Express utils. ensureAuthenticated and redirectToRememberedURL
/** @module AuthUtils */
'use strict';
var _ = require('lodash');
var response = require('./response');
module.exports = {
ensureAuthenticated: function(loginUrl){
return function(req, res, next){
// We store the original requested URL in session to redirect
// him/her back after successful authentication, in case
// he/she is not actually authenticated.
req.session.originalUrl = req.originalUrl;
if (req.isAuthenticated()){
// Pass control to the next middleware
return next();
} else {
// Redirect to auth route.
return res.redirect(loginUrl);
}
};
},
redirectToOriginalURL: function (req, res, next){
res.redirect(req.session.originalUrl || '/');
delete req.session.originalUrl;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment