authentication.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /* global WIKI */
  2. // ------------------------------------
  3. // Local Account
  4. // ------------------------------------
  5. const LocalStrategy = require('passport-local').Strategy
  6. module.exports = {
  7. init (passport, conf) {
  8. passport.use('local',
  9. new LocalStrategy({
  10. usernameField: 'email',
  11. passwordField: 'password'
  12. }, (uEmail, uPassword, done) => {
  13. WIKI.models.users.query().findOne({
  14. email: uEmail,
  15. providerKey: 'local'
  16. }).then((user) => {
  17. if (user) {
  18. return user.verifyPassword(uPassword).then(() => {
  19. if (!user.isActive) {
  20. done(new WIKI.Error.AuthAccountBanned(), null)
  21. } else if (!user.isVerified) {
  22. done(new WIKI.Error.AuthAccountNotVerified(), null)
  23. } else {
  24. done(null, user)
  25. }
  26. }).catch((err) => {
  27. done(err, null)
  28. })
  29. } else {
  30. done(new WIKI.Error.AuthLoginFailed(), null)
  31. }
  32. }).catch((err) => {
  33. done(err, null)
  34. })
  35. }
  36. ))
  37. }
  38. }