2016-08-17 03:56:08 +00:00
|
|
|
var LocalStrategy = require('passport-local').Strategy;
|
|
|
|
|
|
|
|
module.exports = function(passport, appconfig) {
|
|
|
|
|
|
|
|
// Serialization user methods
|
|
|
|
|
|
|
|
passport.serializeUser(function(user, done) {
|
|
|
|
done(null, user._id);
|
|
|
|
});
|
|
|
|
|
|
|
|
passport.deserializeUser(function(id, done) {
|
2016-08-22 03:18:31 +00:00
|
|
|
let user = db.User.find({ id });
|
|
|
|
if(user) {
|
2016-08-17 03:56:08 +00:00
|
|
|
done(null, user);
|
2016-08-22 03:18:31 +00:00
|
|
|
} else {
|
2016-08-17 03:56:08 +00:00
|
|
|
done(err, null);
|
2016-08-22 03:18:31 +00:00
|
|
|
}
|
2016-08-17 03:56:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Setup local user authentication strategy
|
|
|
|
|
|
|
|
passport.use(
|
|
|
|
'local',
|
|
|
|
new LocalStrategy({
|
|
|
|
usernameField : 'email',
|
|
|
|
passwordField : 'password',
|
|
|
|
passReqToCallback : true
|
|
|
|
},
|
|
|
|
function(req, uEmail, uPassword, done) {
|
|
|
|
db.User.findOne({ 'email' : uEmail }).then((user) => {
|
|
|
|
if (user) {
|
|
|
|
user.validatePassword(uPassword).then((isValid) => {
|
|
|
|
return (isValid) ? done(null, user) : done(null, false);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return done(null, false);
|
|
|
|
}
|
|
|
|
}).catch((err) => {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check for admin access
|
|
|
|
|
2016-08-22 03:18:31 +00:00
|
|
|
db.onReady.then(() => {
|
2016-08-17 03:56:08 +00:00
|
|
|
|
2016-08-22 03:18:31 +00:00
|
|
|
if(db.User.count() < 1) {
|
|
|
|
winston.info('No administrator account found. Creating a new one...');
|
|
|
|
if(db.User.insert({
|
|
|
|
email: appconfig.admin,
|
|
|
|
firstName: "Admin",
|
|
|
|
lastName: "Admin",
|
|
|
|
password: "admin123"
|
|
|
|
})) {
|
|
|
|
winston.info('Administrator account created successfully!');
|
|
|
|
} else {
|
|
|
|
winston.error('An error occured while creating administrator account: ');
|
2016-08-17 03:56:08 +00:00
|
|
|
}
|
2016-08-22 03:18:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2016-08-17 03:56:08 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|