Last active
September 2, 2020 20:03
-
-
Save langhard/6790602 to your computer and use it in GitHub Desktop.
Node.js, Passport, SequelizeJS, LocalStrategy, crypto (md5)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** ********************************************************* | |
* 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 | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** ********************************************************* | |
* 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