Progress in implementing passport
This commit is contained in:
30
api/controllers/UserController.js
Normal file
30
api/controllers/UserController.js
Normal file
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* UserController
|
||||
*
|
||||
* @module :: Controller
|
||||
* @description :: A set of functions called `actions`.
|
||||
*
|
||||
* Actions contain code telling Sails how to respond to a certain type of request.
|
||||
* (i.e. do stuff, then send some JSON, show an HTML page, or redirect to another URL)
|
||||
*
|
||||
* You can configure the blueprint URLs which trigger these actions (`config/controllers.js`)
|
||||
* and/or override them with custom routes (`config/routes.js`)
|
||||
*
|
||||
* NOTE: The code you write here supports both HTTP and Socket.io automatically.
|
||||
*
|
||||
* @docs :: http://sailsjs.org/#!documentation/controllers
|
||||
*/
|
||||
|
||||
module.exports = {
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Overrides for the settings in `config/controllers.js`
|
||||
* (specific to UserController)
|
||||
*/
|
||||
_config: {}
|
||||
|
||||
|
||||
};
|
||||
45
api/models/User.js
Normal file
45
api/models/User.js
Normal file
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* User
|
||||
*
|
||||
* @module :: Model
|
||||
* @description :: A short summary of how this model works and what it represents.
|
||||
* @docs :: http://sailsjs.org/#!documentation/models
|
||||
*/
|
||||
|
||||
var bcrypt = require('bcrypt');
|
||||
|
||||
module.exports = {
|
||||
|
||||
attributes: {
|
||||
username: {
|
||||
type: 'string',
|
||||
required: true,
|
||||
unique: true
|
||||
},
|
||||
password: {
|
||||
type: 'string',
|
||||
required: true
|
||||
},
|
||||
toJSON: function() {
|
||||
var obj = this.toObject();
|
||||
delete obj.password;
|
||||
return obj;
|
||||
}
|
||||
},
|
||||
|
||||
beforeCreate: function(user, cb) {
|
||||
bcrypt.genSalt(10, function(err, salt) {
|
||||
bcrypt.hash(user.password, salt, function(err, hash) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
cb(err);
|
||||
}
|
||||
else {
|
||||
user.password = hash;
|
||||
cb(null, user);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
@@ -0,0 +1,43 @@
|
||||
var passport = require('passport'),
|
||||
LocalStrategy = require('passport-local').Strategy,
|
||||
bcrypt = require('bcrypt');
|
||||
|
||||
passport.serializeUser(function(user, done) {
|
||||
done(null, user[0].id);
|
||||
});
|
||||
|
||||
passport.deserializeUser(function(id, done) {
|
||||
User.findById(id, function(err, user) {
|
||||
done(err, user);
|
||||
});
|
||||
});
|
||||
|
||||
passport.use(new LocalStrategy(
|
||||
function(username, password, done) {
|
||||
User.findByUsername(username).done(function(err, user) {
|
||||
if (err) {
|
||||
return done(null, err);
|
||||
}
|
||||
if (!user || user.length < 1) {
|
||||
return done(null, false, {
|
||||
message: 'Incorrect User'
|
||||
});
|
||||
}
|
||||
bcrypt.compare(password, user[0].password, function(err, res) {
|
||||
if (!res) return done(null, false, {
|
||||
message: 'Invalid Password'
|
||||
});
|
||||
return done(null, user);
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
module.exports = {
|
||||
express: {
|
||||
customMiddleware: function(app) {
|
||||
console.log('express midleware for passport');
|
||||
app.use(passport.initialize());
|
||||
app.use(passport.session());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -25,7 +25,9 @@
|
||||
"grunt-contrib-coffee": "~0.10.1",
|
||||
"grunt-sails-linker": "~0.9.5",
|
||||
"grunt-contrib-jade": "~0.10.0",
|
||||
"passport": "0.2.0"
|
||||
"passport": "0.2.0",
|
||||
"passport-local": "1.0.0",
|
||||
"bcrypt": "0.7.8"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "node app.js",
|
||||
|
||||
Reference in New Issue
Block a user