Skip to content

Instantly share code, notes, and snippets.

@hengkiardo
Forked from langhard/model-user.js
Created October 2, 2013 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hengkiardo/6791609 to your computer and use it in GitHub Desktop.
Save hengkiardo/6791609 to your computer and use it in GitHub Desktop.
/** *********************************************************
* 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