From 480d8e4e1e65490a97d362a1728f722b337aad9c Mon Sep 17 00:00:00 2001 From: wayne-o Date: Mon, 23 Feb 2015 18:35:40 +0000 Subject: [PATCH 1/4] adding /register endpoint --- lib/controllers/actions/login.js | 30 ++++++++++-------- lib/controllers/actions/register.js | 40 ++++++++++++++++++++++++ lib/controllers/index.js | 3 +- lib/scope.js | 47 +++++++++++++++++++++-------- test/controllers/index.test.js | 7 ++++- 5 files changed, 100 insertions(+), 27 deletions(-) create mode 100644 lib/controllers/actions/register.js diff --git a/lib/controllers/actions/login.js b/lib/controllers/actions/login.js index d5bc74e..862b294 100644 --- a/lib/controllers/actions/login.js +++ b/lib/controllers/actions/login.js @@ -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' + }); } }); } -}; \ No newline at end of file +}; diff --git a/lib/controllers/actions/register.js b/lib/controllers/actions/register.js new file mode 100644 index 0000000..053d736 --- /dev/null +++ b/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' + }); + } + }); + + } +}; diff --git a/lib/controllers/index.js b/lib/controllers/index.js index c12e608..74ce976 100644 --- a/lib/controllers/index.js +++ b/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') -}; \ No newline at end of file +}; diff --git a/lib/scope.js b/lib/scope.js index 73db403..1163e50 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -7,35 +7,56 @@ 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 attr = { + password: attributes.password + }; + attr[scopeKey] = attributes[scopeKey]; + + var criteria = {}; + criteria[scopeKey] = attr[scopeKey]; + + this.engine.findAuth(criteria, function(err, user) { + if (user) { + cb(); + } + this.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); - } + } } }; -} \ No newline at end of file +} diff --git a/test/controllers/index.test.js b/test/controllers/index.test.js index f79787f..e52db7b 100644 --- a/test/controllers/index.test.js +++ b/test/controllers/index.test.js @@ -14,4 +14,9 @@ describe('controller index', function(){ index.logout.should.be.Function; done(); }); -}) \ No newline at end of file + it('should export register', function(done){ + index.should.have.property('login'); + index.login.should.be.Function; + done(); + }); +}) From 8d74332dfbe3b69ae9f250cb3e52dee8b2ec9d9e Mon Sep 17 00:00:00 2001 From: wayne-o Date: Wed, 25 Feb 2015 07:30:56 +0000 Subject: [PATCH 2/4] registration complete --- lib/controllers/actions/register.js | 2 ++ lib/scope.js | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/controllers/actions/register.js b/lib/controllers/actions/register.js index 053d736..378c69b 100644 --- a/lib/controllers/actions/register.js +++ b/lib/controllers/actions/register.js @@ -16,6 +16,8 @@ module.exports = function(req, res) { } else { var pass = params.password; + waterlock.logger.info('lets do this'); + scope.registerUserAuthObject(params, req, function(err, user) { if (err) { res.serverError(err); diff --git a/lib/scope.js b/lib/scope.js index 1163e50..a6cbd14 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -26,6 +26,7 @@ function generateScope(scopeKey, engine) { engine: engine, registerUserAuthObject: function(attributes, req, cb) { + var self = this; var attr = { password: attributes.password }; @@ -38,7 +39,7 @@ function generateScope(scopeKey, engine) { if (user) { cb(); } - this.engine.findOrCreateAuth(criteria, attr, cb); + self.engine.findOrCreateAuth(criteria, attr, cb); }); }, From 4769ae4bcd411bcb610308fa3f03a17063f69d99 Mon Sep 17 00:00:00 2001 From: wayne-o Date: Wed, 25 Feb 2015 07:32:09 +0000 Subject: [PATCH 3/4] removing logging --- lib/controllers/actions/register.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/controllers/actions/register.js b/lib/controllers/actions/register.js index 378c69b..053d736 100644 --- a/lib/controllers/actions/register.js +++ b/lib/controllers/actions/register.js @@ -16,8 +16,6 @@ module.exports = function(req, res) { } else { var pass = params.password; - waterlock.logger.info('lets do this'); - scope.registerUserAuthObject(params, req, function(err, user) { if (err) { res.serverError(err); From 6125bc5af0f3deba684b9156d6eb1894c8620781 Mon Sep 17 00:00:00 2001 From: wayne-o Date: Fri, 29 May 2015 17:29:22 +0100 Subject: [PATCH 4/4] trying to sendgrid --- .DS_Store | Bin 0 -> 6148 bytes lib/waterlock-local-auth.js | 11 ++++++----- package.json | 13 +++++++------ test/waterlock.js | 23 +++++++++++------------ 4 files changed, 24 insertions(+), 23 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..3c592387d73f2f3c591ce56ec87ed8a73979509f GIT binary patch literal 6148 zcmeHK&5GMF5SEg(>m(O1?LwdzT?;v6aYN|Mo2JJ?z)M1DN@`=%2#g(KC!vIZ&v}gg zzsIus4tY{W|=XgZ#0^btWQR=7-PIU4=)+B8DkbGVyOznZv@*>kECQg zh#dRKra_zrQ2QB7kD|%(KQe%8S7tGjEC`nS>le!`Z?%5#Qn~Wr;h9yf)$y^Q`-troL=>>`E`cZvx&{l4&;r7B zDxgl~=8C~}I{1Z&a}5?6bvol}Wa!6?%*_pjtI@$PR5;_VMk", "contributors": [ diff --git a/test/waterlock.js b/test/waterlock.js index c268120..172a802 100644 --- a/test/waterlock.js +++ b/test/waterlock.js @@ -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. @@ -27,7 +27,6 @@ module.exports.waterlock = { passwordReset:{ tokens: false, mail: { - protocol: "SMTP", options:{ service: "Gmail", auth: { @@ -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:{} @@ -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:{ @@ -63,4 +62,4 @@ module.exports.waterlock = { audience: "app name", subject: "subject" } -} \ No newline at end of file +}