Skip to content

Commit

Permalink
create, usernameexists and emailexists operation. Standard way to sen…
Browse files Browse the repository at this point in the history
…d responses to the client
  • Loading branch information
Nuno Silva committed Feb 13, 2015
1 parent 14476bf commit 044c735
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 35 deletions.
46 changes: 31 additions & 15 deletions auth.js
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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)
);
});

Expand Down
41 changes: 35 additions & 6 deletions src/authentication.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 }
);
});
}


Expand Down
29 changes: 29 additions & 0 deletions src/response.js
Original file line number Diff line number Diff line change
@@ -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();
32 changes: 18 additions & 14 deletions src/userdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

//----------------------
Expand Down
63 changes: 63 additions & 0 deletions test/userdbtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,69 @@ describe('userdbTests', function() {
});
}),

it('create and get user by email test (no password)', function(done) {
var testUser = {
email : "[email protected]",
password : md5("password"),
name : "Nuno Silva",
username : "nunos"
};
userdb.add(testUser, function(err, data){
userdb.getByEmail("[email protected]", null, function(err, data){
data.email.should.equal("[email protected]");
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 : "[email protected]",
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("[email protected]");
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 : "[email protected]",
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("[email protected]");
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 : "[email protected]",
Expand Down

0 comments on commit 044c735

Please sign in to comment.