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.

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