Skip to content

Instantly share code, notes, and snippets.

@langhard
Last active September 2, 2020 20:03
Show Gist options
  • Save langhard/6790602 to your computer and use it in GitHub Desktop.
Save langhard/6790602 to your computer and use it in GitHub Desktop.
Node.js, Passport, SequelizeJS, LocalStrategy, crypto (md5)
/** *********************************************************
* API - Model - User
********************************************************* */
module.exports = function (sequelize, DataTypes) {
return sequelize.define('User', {
username: DataTypes.STRING,
password: DataTypes.STRING,
firstName: DataTypes.STRING,
lastName: DataTypes.STRING,
email: DataTypes.STRING
})
}
/** *********************************************************
* PassportJS
********************************************************* */
passport.use(new LocalStrategy(
function (username, password, done) {
Model.User.find({ where: {username: username, password: crypto.createHash('md5').update(password).digest("hex") } })
.success(function (user) {
if (user !== null) {
console.log('[AUTH] Success with username: ' + user.username + ' and password (md5-hash): ' + user.password);
return done(null, user);
}
else {
console.log('[AUTH] Error with username: ' + username + ' and password:' + password);
console.log('[AUTH] md5-hash of passed password: ' + crypto.createHash('md5').update(password).digest("hex"));
return done(null, false);
}
})
}
));
// Serialized and deserialized methods when got from session
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (user, done) {
done(null, user);
});
// Define a middleware function to be used for every secured routes
var auth = function (req, res, next) {
if (!req.isAuthenticated()){
console.log('[AUTH] Error 401');
res.send(401);
}else{
console.log('[AUTH] Success by Session');
next();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment