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.

64 lines
1.9 KiB

  1. var LocalStrategy = require('passport-local').Strategy;
  2. module.exports = function(passport, appconfig) {
  3. // Serialization user methods
  4. passport.serializeUser(function(user, done) {
  5. done(null, user._id);
  6. });
  7. passport.deserializeUser(function(id, done) {
  8. db.User.findById(id).then((user) => {
  9. done(null, user);
  10. }).catch((err) => {
  11. done(err, null);
  12. });
  13. });
  14. // Setup local user authentication strategy
  15. passport.use(
  16. 'local',
  17. new LocalStrategy({
  18. usernameField : 'email',
  19. passwordField : 'password',
  20. passReqToCallback : true
  21. },
  22. function(req, uEmail, uPassword, done) {
  23. db.User.findOne({ 'email' : uEmail }).then((user) => {
  24. if (user) {
  25. user.validatePassword(uPassword).then((isValid) => {
  26. return (isValid) ? done(null, user) : done(null, false);
  27. });
  28. } else {
  29. return done(null, false);
  30. }
  31. }).catch((err) => {
  32. done(err);
  33. });
  34. })
  35. );
  36. // Check for admin access
  37. db.connectPromise.then(() => {
  38. db.User.count().then((count) => {
  39. if(count < 1) {
  40. winston.info('No administrator account found. Creating a new one...');
  41. db.User.new({
  42. email: appconfig.admin,
  43. firstName: "Admin",
  44. lastName: "Admin",
  45. password: "admin123"
  46. }).then(() => {
  47. winston.info('Administrator account created successfully!');
  48. }).catch((ex) => {
  49. winston.error('An error occured while creating administrator account: ' + ex);
  50. });
  51. }
  52. });
  53. });
  54. };