Skip to content

Instantly share code, notes, and snippets.

@BerkeleyTrue
Last active April 13, 2016 05:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BerkeleyTrue/426147e743cb3094148dc96abf1790c6 to your computer and use it in GitHub Desktop.
Save BerkeleyTrue/426147e743cb3094148dc96abf1790c6 to your computer and use it in GitHub Desktop.
Free Code Camp's new deserializer
const fields = {
progressTimestamps: false,
completedChallenges: false,
challengeMap: false
};
passport.deserializeUser((id, done) => {
// We use a combineLatest here to run both of these
// database request in parallel
Observable.combineLatest(
// `findById$` notice the dolar sign?
// It's a convention we use to indicate that
// this function returns an Observable
// DB query 1:
// This request attempts to find the user
// You may notice the second arg, it is an object
// with the `fields` property populated by the object
// defined above. This tells loopback we want to filter out those
// fields from the request
this.userModel.findById$(id, { fields }),
// DB query 2: Some secret souce :)
this.userModel.getPointsById$(id),
// Attach those points to the user object when
// both queries have returned
(user, points) => {
if (user) { user.points = points; }
return user;
}
)
.doOnNext(user => {
// make sure the user actually exists
// if not, then we throw an error. This is caught by RxJS
// and sent down the error pipeline
if (!user) { throw new Error('deserialize found no user'); }
})
.subscribe(
user => done(null, user),
// this is where errors are sent
// to be handled downstream by our custom
// express error handlers
done
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment