Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@AndrejGajdos
Last active June 24, 2018 07:38
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 AndrejGajdos/ea6a481a498dcb47b61bda9a0885d12d to your computer and use it in GitHub Desktop.
Save AndrejGajdos/ea6a481a498dcb47b61bda9a0885d12d to your computer and use it in GitHub Desktop.
Adding required libraries and modules for user authentication, adding serializing and de-serializing the user information to the session. https://github.com/AndrejGajdos/auth-flow-spa-node-react/blob/master/script/controllers/auth.js
const bcrypt = require('bcrypt');
const passport = require('koa-passport');
const FacebookStrategy = require('passport-facebook').Strategy;
const LocalStrategy = require('passport-local').Strategy;
const config = require('../serverConfig');
const { db } = require('../server');
const { promisify } = require('util');
const getAsync = promisify(db.get).bind(db);
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser(async (id, done) => {
try {
let user = null;
await getAsync('usersMockDatabase').then((users) => {
user = JSON.parse(users).find(currUser => currUser.id === id);
});
if (user) {
done(null, user);
} else {
done(null, false);
}
} catch (err) {
done(err);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment