37 lines
1.0 KiB
JavaScript
37 lines
1.0 KiB
JavaScript
|
const database = require('./../database');
|
||
|
const passport = require('passport');
|
||
|
const passportLocal = require('passport-local');
|
||
|
|
||
|
passport.use(new passportLocal.Strategy((email, password, cb) => {
|
||
|
query = `SELECT id, email, password, admin
|
||
|
FROM accounts.users
|
||
|
WHERE email = $1`;
|
||
|
const result = database.executeQuery(query, [email]);
|
||
|
if(result.length > 0) {
|
||
|
const first = result[0];
|
||
|
bcrypt.compare(password, first[2], function(err, res) {
|
||
|
if(res) {
|
||
|
cb(null, { id: first[0], email: first[1], admin: first[3] })
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
cb(null, false)
|
||
|
}
|
||
|
})
|
||
|
} else {
|
||
|
cb(null, false)
|
||
|
}
|
||
|
}));
|
||
|
|
||
|
passport.serializeUser((user, done) => {
|
||
|
done(null, user.id)
|
||
|
})
|
||
|
|
||
|
passport.deserializeUser((id, cb) => {
|
||
|
query = `SELECT id, email, admin
|
||
|
FROM accounts.users
|
||
|
WHERE id = $1`;
|
||
|
const result = database.executeQuery(query, [parseInt(id, 10)]);
|
||
|
|
||
|
cb(null, result[0]);
|
||
|
});
|