You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wiki/server/modules/authentication/local/authentication.js

44 lines
1.3 KiB

/* global WIKI */
const bcrypt = require('bcryptjs-then')
// ------------------------------------
// Local Account
// ------------------------------------
const LocalStrategy = require('passport-local').Strategy
module.exports = {
init (passport, conf) {
passport.use(conf.key,
new LocalStrategy({
usernameField: 'email',
passwordField: 'password'
}, async (uEmail, uPassword, done) => {
try {
const user = await WIKI.models.users.query().findOne({
email: uEmail.toLowerCase()
})
if (user) {
const authStrategyData = user.auth[conf.key]
if (!authStrategyData) {
throw new WIKI.Error.AuthLoginFailed()
} else if (await bcrypt.compare(uPassword, authStrategyData.password) !== true) {
throw new WIKI.Error.AuthLoginFailed()
} else if (!user.isActive) {
throw new WIKI.Error.AuthAccountBanned()
} else if (!user.isVerified) {
throw new WIKI.Error.AuthAccountNotVerified()
} else {
done(null, user)
}
} else {
throw new WIKI.Error.AuthLoginFailed()
}
} catch (err) {
done(err, null)
}
})
)
}
}