aboutsummaryrefslogtreecommitdiff
path: root/node_modules/passport/lib/middleware/initialize.js
diff options
context:
space:
mode:
authornanalelfe <nargiza.nosirova@mail.utoronto.ca>2016-07-21 06:29:31 +0000
committernanalelfe <nargiza.nosirova@mail.utoronto.ca>2016-07-21 06:29:31 +0000
commitee8e1a13b60a6adfdc691b2a9b57289188397641 (patch)
tree096633208d9b8b6b59b67f4034a0cbb41e1f4c5d /node_modules/passport/lib/middleware/initialize.js
parent689df70a38ace2f88cfef6ab50f10dc546b48f00 (diff)
need pull
Diffstat (limited to 'node_modules/passport/lib/middleware/initialize.js')
-rw-r--r--node_modules/passport/lib/middleware/initialize.js55
1 files changed, 55 insertions, 0 deletions
diff --git a/node_modules/passport/lib/middleware/initialize.js b/node_modules/passport/lib/middleware/initialize.js
new file mode 100644
index 0000000..53ce3d8
--- /dev/null
+++ b/node_modules/passport/lib/middleware/initialize.js
@@ -0,0 +1,55 @@
+/**
+ * Passport initialization.
+ *
+ * Intializes Passport for incoming requests, allowing authentication strategies
+ * to be applied.
+ *
+ * If sessions are being utilized, applications must set up Passport with
+ * functions to serialize a user into and out of a session. For example, a
+ * common pattern is to serialize just the user ID into the session (due to the
+ * fact that it is desirable to store the minimum amount of data in a session).
+ * When a subsequent request arrives for the session, the full User object can
+ * be loaded from the database by ID.
+ *
+ * Note that additional middleware is required to persist login state, so we
+ * must use the `connect.session()` middleware _before_ `passport.initialize()`.
+ *
+ * If sessions are being used, this middleware must be in use by the
+ * Connect/Express application for Passport to operate. If the application is
+ * entirely stateless (not using sessions), this middleware is not necessary,
+ * but its use will not have any adverse impact.
+ *
+ * Examples:
+ *
+ * app.use(connect.cookieParser());
+ * app.use(connect.session({ secret: 'keyboard cat' }));
+ * app.use(passport.initialize());
+ * app.use(passport.session());
+ *
+ * passport.serializeUser(function(user, done) {
+ * done(null, user.id);
+ * });
+ *
+ * passport.deserializeUser(function(id, done) {
+ * User.findById(id, function (err, user) {
+ * done(err, user);
+ * });
+ * });
+ *
+ * @return {Function}
+ * @api public
+ */
+module.exports = function initialize(passport) {
+
+ return function initialize(req, res, next) {
+ req._passport = {};
+ req._passport.instance = passport;
+
+ if (req.session && req.session[passport._key]) {
+ // load data from existing session
+ req._passport.session = req.session[passport._key];
+ }
+
+ next();
+ };
+};