From 044c73562de0b2592620ea3900c45a362797fb90 Mon Sep 17 00:00:00 2001 From: Nuno Silva Date: Fri, 13 Feb 2015 22:10:22 +0000 Subject: [PATCH] create, usernameexists and emailexists operation. Standard way to send responses to the client --- auth.js | 46 ++++++++++++++++++++----------- src/authentication.js | 41 +++++++++++++++++++++++----- src/response.js | 29 ++++++++++++++++++++ src/userdb.js | 32 ++++++++++++---------- test/userdbtest.js | 63 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 176 insertions(+), 35 deletions(-) create mode 100644 src/response.js diff --git a/auth.js b/auth.js index 941d866..2199f6f 100644 --- a/auth.js +++ b/auth.js @@ -1,6 +1,7 @@ var restify = require("restify"); var authentication = require("./src/authentication.js"); +var response = require("./src/response.js"); var server = restify.createServer({ name: 'orionsbeltauth', @@ -11,53 +12,68 @@ server.use(restify.acceptParser(server.acceptable)); server.use(restify.queryParser()); server.use(restify.bodyParser()); -function buildError(next, message){ - return next(new restify.InvalidArgumentError(message)); -} - -function createContext(req, res, next){ +function createContext(req, res){ console.log("creating authentication context"); return { username : req.params.username, email : req.params.email, password : req.params.password, - next : next, - callback : function(user){ - res.send(200, user) - } + res : res } } server.post("/login", function (req, res, next) { if( req.params == null){ - return buildError(next,'No parameters were supplied'); + return response.sendError('No parameters were supplied'); } console.log("Checking invalid username and email"); if( req.params.username == null && req.params.email == null){ - return buildError(next,'Username or email are required'); + return response.sendError(res,'Username or email are required'); } console.log("Checking invalid password"); if( req.params.password == null){ - return buildError(next,'Password is required'); + return response.sendError(res,'Password is required'); } console.log("calling authentication::login"); authentication.login( - createContext(req,res,next) + createContext(req,res) ); }); server.post("/create", function (req, res, next) { if( req.params == null){ - return buildError(next,'No parameters were supplied'); + return response.sendError(res,'No parameters were supplied'); } console.log("calling authentication::create"); authentication.create( - createContext(req,res,next) + createContext(req,res) + ); +}); + +server.post("/usernameexists", function (req, res, next) { + if( req.params == null){ + return response.sendError(res,'No parameters were supplied'); + } + + console.log("calling authentication::usernameexists"); + authentication.userNameExists( + createContext(req,res) + ); +}); + +server.post("/emailexists", function (req, res, next) { + if( req.params == null){ + return response.sendError(res,'No parameters were supplied'); + } + + console.log("calling authentication::emailExists"); + authentication.emailExists( + createContext(req,res) ); }); diff --git a/src/authentication.js b/src/authentication.js index 79bf64b..985c411 100644 --- a/src/authentication.js +++ b/src/authentication.js @@ -1,20 +1,23 @@ var restify = require('restify'); var userdb = require('./userdb.js'); +var response = require('./response.js'); function processLoginResult(error, data, context){ - console.log("getByUsername: " + JSON.stringify(data)); if (data == null) { - return context.next(new restify.InvalidArgumentError("No data was returned!")); + return response.sendError(context.res,"No data was returned!"); } if (error != null) { - return context.next(new restify.InvalidArgumentError(JSON.stringify(error))); + return response.sendError(JSON.stringify(error)); } - context.callback({ + + var data = { id : data._id, username : data.username, name : data.name, email : data.email - }); + }; + + response.sendSuccess(context.res,data); } Authentication.prototype.login = function(context) { @@ -30,7 +33,33 @@ Authentication.prototype.login = function(context) { } Authentication.prototype.create = function(context) { - + var user = { + email : context.email, + password : context.password, + name : context.name, + username : context.username + }; + userdb.add(user, function(err, data){ + response.sendSuccess(context.res); + }); +} + +Authentication.prototype.userNameExists = function(context) { + userdb.getByUsername(context.username, null, function(err, data){ + response.sendSuccess( + context.res, + { exists : data != null } + ); + }); +} + +Authentication.prototype.emailExists = function(context) { + userdb.getByEmail(context.email, null, function(err, data){ + response.sendSuccess( + context.res, + { exists : data != null } + ); + }); } diff --git a/src/response.js b/src/response.js new file mode 100644 index 0000000..9c1c2c4 --- /dev/null +++ b/src/response.js @@ -0,0 +1,29 @@ +Response.prototype.sendSuccess = function(res, data){ + var response = { + success : true + }; + + if( data != null ){ + response.data = data; + } + + res.send(200, response); +} + +Response.prototype.sendError = function(res, message){ + var response = { + success : false, + message : message + }; + + res.send(200, response); +} + +//---------------------- +// Constructor +//---------------------- + +function Response() { +} + +module.exports = new Response(); diff --git a/src/userdb.js b/src/userdb.js index 3b8e489..653df0a 100644 --- a/src/userdb.js +++ b/src/userdb.js @@ -19,23 +19,27 @@ UserDB.prototype.delete = function(userId, callback) { } UserDB.prototype.getByEmail = function(userEmail,userPassword, callback) { - db.users.findOne( - { - email : userEmail, - password : md5(userPassword), - }, - callback - ); + var info = { + email : userEmail + }; + + if(userPassword != null){ + info.password = md5(userPassword); + } + + db.users.findOne(info, callback); } UserDB.prototype.getByUsername = function(userName,userPassword, callback) { - db.users.findOne( - { - username : userName, - password : md5(userPassword), - }, - callback -); + var info = { + username : userName + }; + + if(userPassword != null){ + info.password = md5(userPassword); + } + + db.users.findOne(info,callback); } //---------------------- diff --git a/test/userdbtest.js b/test/userdbtest.js index 659da99..af7ebed 100644 --- a/test/userdbtest.js +++ b/test/userdbtest.js @@ -26,6 +26,69 @@ describe('userdbTests', function() { }); }), + it('create and get user by email test (no password)', function(done) { + var testUser = { + email : "nunos@zi-yu.com", + password : md5("password"), + name : "Nuno Silva", + username : "nunos" + }; + userdb.add(testUser, function(err, data){ + userdb.getByEmail("nunos@zi-yu.com", null, function(err, data){ + data.email.should.equal("nunos@zi-yu.com"); + data.password.should.equal(md5("password")); + data.name.should.equal("Nuno Silva"); + data.username.should.equal("nunos"); + + userdb.delete(data._id, function(err, data){ + done(); + }); + }); + }); + }), + + it('create and get user by username test', function(done) { + var testUser = { + email : "nunos@zi-yu.com", + password : md5("password"), + name : "Nuno Silva", + username : "nunos" + }; + userdb.add(testUser, function(err, data){ + userdb.getByUsername("nunos", "password", function(err, data){ + data.email.should.equal("nunos@zi-yu.com"); + data.password.should.equal(md5("password")); + data.name.should.equal("Nuno Silva"); + data.username.should.equal("nunos"); + + userdb.delete(data._id, function(err, data){ + done(); + }); + }); + }); + }), + + it('create and get user by username test (no password)', function(done) { + var testUser = { + email : "nunos@zi-yu.com", + password : md5("password"), + name : "Nuno Silva", + username : "nunos" + }; + userdb.add(testUser, function(err, data){ + userdb.getByUsername("nunos", null, function(err, data){ + data.email.should.equal("nunos@zi-yu.com"); + data.password.should.equal(md5("password")); + data.name.should.equal("Nuno Silva"); + data.username.should.equal("nunos"); + + userdb.delete(data._id, function(err, data){ + done(); + }); + }); + }); + }), + it('update user by email test', function(done) { var testUser = { email : "nunos@zi-yu.com",