Skip to content

Instantly share code, notes, and snippets.

@daffl
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save daffl/ac19c17770d80da0f15f to your computer and use it in GitHub Desktop.

Select an option

Save daffl/ac19c17770d80da0f15f to your computer and use it in GitHub Desktop.
var userService = mongodb({
db: 'feathers-demo',
collection: 'users'
}).extend({
authenticate: function(username, password, callback) {
// This will be used as the MongoDB query
var query = {
username: username
};
this.find({ query: query }, function(error, users) {
if(error) {
return callback(error);
}
var user = users[0];
if(!user) {
return callback(new Error('No user found'));
}
// Compare the hashed and salted passwords
if(user.password !== hash(password, user.salt)) {
return callback(new Error('User password does not match'));
}
// If we got to here, we call the callback with the user information
return callback(null, user);
});
}
});
app.configure(feathers.socketio(function(io) {
io.on('connection', function(socket){
io.on('login', function(username, password, callback) {
app.service('users').authenticate(username, password, function(error, user) {
socket.feathers.user = user;
callback(error, user);
});
});
});
}).use(userService);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment