Last active
August 29, 2015 14:11
-
-
Save andresmatasuarez/af2c02ee0b35650faa63 to your computer and use it in GitHub Desktop.
Passport Express utils. ensureAuthenticated and redirectToRememberedURL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @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