Last active
June 24, 2018 07:38
-
-
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
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
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