mirror of https://github.com/requarks/wiki
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.
66 lines
1.8 KiB
66 lines
1.8 KiB
8 years ago
|
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) {
|
||
8 years ago
|
let user = db.User.find({ id });
|
||
|
if(user) {
|
||
8 years ago
|
done(null, user);
|
||
8 years ago
|
} else {
|
||
8 years ago
|
done(err, null);
|
||
8 years ago
|
}
|
||
8 years ago
|
});
|
||
|
|
||
|
// 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
|
||
|
|
||
8 years ago
|
db.onReady.then(() => {
|
||
8 years ago
|
|
||
8 years ago
|
/*if(db.User.count() < 1) {
|
||
8 years ago
|
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: ');
|
||
8 years ago
|
}
|
||
8 years ago
|
}*/
|
||
8 years ago
|
|
||
|
return true;
|
||
8 years ago
|
|
||
|
});
|
||
|
|
||
|
};
|