authentication.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* global WIKI */
  2. const bcrypt = require('bcryptjs-then')
  3. // ------------------------------------
  4. // Local Account
  5. // ------------------------------------
  6. const LocalStrategy = require('passport-local').Strategy
  7. module.exports = {
  8. init (passport, conf) {
  9. passport.use(conf.key,
  10. new LocalStrategy({
  11. usernameField: 'email',
  12. passwordField: 'password'
  13. }, async (uEmail, uPassword, done) => {
  14. try {
  15. const user = await WIKI.db.users.query().findOne({
  16. email: uEmail.toLowerCase()
  17. })
  18. if (user) {
  19. const authStrategyData = user.auth[conf.key]
  20. if (!authStrategyData) {
  21. throw new WIKI.Error.AuthLoginFailed()
  22. } else if (await bcrypt.compare(uPassword, authStrategyData.password) !== true) {
  23. throw new WIKI.Error.AuthLoginFailed()
  24. } else if (!user.isActive) {
  25. throw new WIKI.Error.AuthAccountBanned()
  26. } else if (!user.isVerified) {
  27. throw new WIKI.Error.AuthAccountNotVerified()
  28. } else {
  29. done(null, user)
  30. }
  31. } else {
  32. throw new WIKI.Error.AuthLoginFailed()
  33. }
  34. } catch (err) {
  35. done(err, null)
  36. }
  37. })
  38. )
  39. }
  40. }