Skip to content

Instantly share code, notes, and snippets.

@rfaisal
Created April 1, 2014 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rfaisal/9906511 to your computer and use it in GitHub Desktop.
Save rfaisal/9906511 to your computer and use it in GitHub Desktop.
var https = require('https'),
querystring = require('querystring'),
jwt = require('../shared/jwt'),
user_db = [{
user_name: "rfaisal",
password: "123456",
user_id: "8798af78d"
}];
exports.register = function (api) {
api.post('self', self);
};
function self(request, response) {
if (request.body.user_name && request.body.password) {
//replace the following logic with a database operation
var index = -1;
for (var i = 0; i < user_db.length; i++) {
if (user_db[i].user_name == request.body.user_name && user_db[i].password == request.body.password) {
index = i;
}
}
if (index === -1) {
response.send(statusCodes.UNAUTHORIZED, {
code: 401,
error: "Cannot authenticate user."
});
} else { //user_name and password is verified
var current_user = user_db[index];
response.send(statusCodes.OK, {
"user": {
"userId": 'Self:' + current_user.user_id
},
"authenticationToken": jwt.createZumoJwt(request.service.config.masterKey, 4, 'Self', 'Self:' + current_user.user_id, {
userId: 'Self:' + current_user.user_id,
userName: current_user.user_name
})
});
}
} else {
response.send(statusCodes.UNAUTHORIZED, {
code: 401,
error: "Both user_name and password must be provided"
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment