Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding /register endpoint to local-auth #23

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
30 changes: 18 additions & 12 deletions lib/controllers/actions/login.js
Expand Up @@ -4,29 +4,35 @@ var bcrypt = require('bcrypt');
/**
* Login action
*/
module.exports = function(req, res){
module.exports = function(req, res) {

var scope = require('../../scope')(waterlock.Auth, waterlock.engine);
var params = req.params.all();

if(typeof params[scope.type] === 'undefined' || typeof params.password === 'undefined'){
waterlock.cycle.loginFailure(req, res, null, {error: 'Invalid '+scope.type+' or password'});
}else{

if (typeof params[scope.type] === 'undefined' || typeof params.password === 'undefined') {
waterlock.cycle.loginFailure(req, res, null, {
error: 'Invalid ' + scope.type + ' or password'
});
} else {
var pass = params.password;
scope.getUserAuthObject(params, req, function(err, user){
scope.getUserAuthObject(params, req, function(err, user) {
if (err) {
res.serverError(err);
}
if (user) {
if(bcrypt.compareSync(pass, user.auth.password)){
if (bcrypt.compareSync(pass, user.auth.password)) {
waterlock.cycle.loginSuccess(req, res, user);
}else{
waterlock.cycle.loginFailure(req, res, user, {error: 'Invalid '+scope.type+' or password'});
} else {
waterlock.cycle.loginFailure(req, res, user, {
error: 'Invalid ' + scope.type + ' or password'
});
}
} else {
//TODO redirect to register
waterlock.cycle.loginFailure(req, res, null, {error: 'user not found'});
waterlock.cycle.loginFailure(req, res, null, {
error: 'user not found'
});
}
});
}
};
};
40 changes: 40 additions & 0 deletions lib/controllers/actions/register.js
@@ -0,0 +1,40 @@
'use strict';
var bcrypt = require('bcrypt');

/**
* Login action
*/
module.exports = function(req, res) {

var scope = require('../../scope')(waterlock.Auth, waterlock.engine);
var params = req.params.all();

if (typeof params[scope.type] === 'undefined' || typeof params.password === 'undefined') {
waterlock.cycle.registerFailure(req, res, null, {
error: 'Invalid ' + scope.type + ' or password'
});
} else {
var pass = params.password;

scope.registerUserAuthObject(params, req, function(err, user) {
if (err) {
res.serverError(err);
}
if (user) {
//NOTE: not sure we need to bother with bcrypt here?
if (bcrypt.compareSync(pass, user.auth.password)) {
waterlock.cycle.registerSuccess(req, res, user);
} else {
waterlock.cycle.registerFailure(req, res, user, {
error: 'Invalid ' + scope.type + ' or password'
});
}
} else {
waterlock.cycle.registerFailure(req, res, null, {
error: scope.type + ' is already in use'
});
}
});

}
};
3 changes: 2 additions & 1 deletion lib/controllers/index.js
@@ -1,6 +1,7 @@
exports.login = require('./actions/login');
exports.logout = require('./actions/logout');
exports.register = require('./actions/register');

exports.extras = {
reset: require('./actions/reset')
};
};
48 changes: 35 additions & 13 deletions lib/scope.js
Expand Up @@ -7,35 +7,57 @@ var authConfig = require('./waterlock-local-auth').authConfig;
* @type {Object}
*/

module.exports = function(Auth, engine){
module.exports = function(Auth, engine) {
var def = Auth.definition;
if(typeof def.email !== 'undefined'){

if (typeof def.email !== 'undefined') {
return generateScope('email', engine);
}else if(typeof def.username !== 'undefined'){
} else if (typeof def.username !== 'undefined') {
return generateScope('username', engine);
}else{
} else {
var error = new Error('Auth model must have either an email or username attribute');
throw error;
}
}
};

function generateScope(scopeKey, engine){
function generateScope(scopeKey, engine) {
return {
type: scopeKey,
engine: engine,
getUserAuthObject: function(attributes, req, cb){
var attr = {password: attributes.password};

registerUserAuthObject: function(attributes, req, cb) {
var self = this;
var attr = {
password: attributes.password
};
attr[scopeKey] = attributes[scopeKey];

var criteria = {};
criteria[scopeKey] = attr[scopeKey];

this.engine.findAuth(criteria, function(err, user) {
if (user) {
cb();
}
self.engine.findOrCreateAuth(criteria, attr, cb);
});

},

getUserAuthObject: function(attributes, req, cb) {
var attr = {
password: attributes.password
};
attr[scopeKey] = attributes[scopeKey];

var criteria = {};
criteria[scopeKey] = attr[scopeKey];

if(authConfig.createOnNotFound){
if (authConfig.createOnNotFound) {
this.engine.findOrCreateAuth(criteria, attr, cb);
}else{
} else {
this.engine.findAuth(criteria, cb);
}
}
}
};
}
}
11 changes: 6 additions & 5 deletions lib/waterlock-local-auth.js
Expand Up @@ -38,13 +38,14 @@ exports.config = wlconfig;
*/
exports.authConfig = method;

if(typeof method === 'object' &&
if(typeof method === 'object' &&
typeof method.passwordReset !== 'undefined' &&
method.passwordReset.tokens){
var nodemailer = require('nodemailer');
var mail = method.passwordReset.mail;
var smtpTransport = nodemailer.createTransport(mail.protocol, mail.options);
exports.transport = smtpTransport;
var sgTransport = require('nodemailer-sendgrid-transport');
var mail = method.passwordReset.sgOptions;
var transport = nodemailer.createTransport(sgTransport(mail));
exports.transport = transport;
}

/**
Expand All @@ -57,4 +58,4 @@ exports.actions = require('./controllers');
* [model description]
* @type {[type]}
*/
exports.model = require('./models');
exports.model = require('./models');
13 changes: 7 additions & 6 deletions package.json
Expand Up @@ -16,22 +16,23 @@
"authentication",
"sails"
],
"devDependencies":{
"devDependencies": {
"mocha": "*",
"should": "*",
"proxyquire": "*",
"coveralls": "*",
"istanbul": "*",
"jshint": "*"
},
"dependencies":{
"dependencies": {
"bcrypt": "~0.8.1",
"lodash": "~2.4.1",
"moment": "~2.9.0",
"nodemailer": "~1.3.0",
"jade": "~1.3.1",
"jwt-simple": "~0.2.0",
"node-uuid": "~1.4.2"
"lodash": "~2.4.1",
"moment": "~2.9.0",
"node-uuid": "~1.4.2",
"nodemailer": "^1.3.4",
"nodemailer-sendgrid-transport": "^0.1.0"
},
"author": "David Rivera <david.r.rivera193@gmail.com>",
"contributors": [
Expand Down
7 changes: 6 additions & 1 deletion test/controllers/index.test.js
Expand Up @@ -14,4 +14,9 @@ describe('controller index', function(){
index.logout.should.be.Function;
done();
});
})
it('should export register', function(done){
index.should.have.property('login');
index.login.should.be.Function;
done();
});
})
23 changes: 11 additions & 12 deletions test/waterlock.js
Expand Up @@ -4,20 +4,20 @@
*
* defines various options used by waterlock
* for more informaiton checkout
*
*
* http://waterlock.ninja/documentation
*/
module.exports.waterlock = {

// Base URL
//
//
// used by auth methods for callback URI's using oauth and for password
// reset links.
baseUrl: "http://localhost:1337",
// Auth Method(s)
//
// this can be a single string, an object, or an array of objects for your

// Auth Method(s)
//
// this can be a single string, an object, or an array of objects for your
// chosen auth method(s) you will need to see the individual module's README
// file for more information on the attributes necessary. This is an example
// of the local authentication method with password reset tokens disabled.
Expand All @@ -27,7 +27,6 @@ module.exports.waterlock = {
passwordReset:{
tokens: false,
mail: {
protocol: "SMTP",
options:{
service: "Gmail",
auth: {
Expand All @@ -38,7 +37,7 @@ module.exports.waterlock = {
from: "no-reply@domain.com",
subject: "Your password reset!",
forwardUrl: "http://localhost:1337"
},
},
template:{
file: "../views/email.jade",
vars:{}
Expand All @@ -49,8 +48,8 @@ module.exports.waterlock = {

// JSON Web Tokens
//
// this provides waterlock with basic information to build your tokens,
// these tokens are used for authentication, password reset,
// this provides waterlock with basic information to build your tokens,
// these tokens are used for authentication, password reset,
// and anything else you can imagine
jsonWebTokens:{

Expand All @@ -63,4 +62,4 @@ module.exports.waterlock = {
audience: "app name",
subject: "subject"
}
}
}