Final touches: Pre & Post process User object

Passport requires that we add functions to serialize and deserialize the user:

// Serialize and deserialize user for paqssport
passport.serializeUser(function (user, done) {
  done(null, user);
});

passport.deserializeUser(function (user, done) {
  done(null, user);
});

The serializeUser/deserializeUser pre and post process the user object:

  • The first argument into the function is a user object, and the second is done, a callback.
  • For our purposes, we are just executing the callback and sending it the user object unchanged.
  • If we needed to do more with the user object (e.g. check it agains our database, etc), this could be done here.

These functions are default functions that just output the user to the console, which is a great debugging tool.

Diff
diff --git a/code/index.js b/code/index.js
index 7b66e7c..da2f90b 100644
--- a/code/index.js
+++ b/code/index.js
@@ -30,6 +30,15 @@ const samlStrategy = new saml.Strategy(
 // Tell passport to use the samlStrategy
 passport.use("samlStrategy", samlStrategy);
 
+// Serialize and deserialize user for paqssport
+passport.serializeUser(function (user, done) {
+  done(null, user);
+});
+
+passport.deserializeUser(function (user, done) {
+  done(null, user);
+});
+
 // Initialize express.
 const app = express();