Skip to content

Commit

Permalink
Add show method and improve Incoming controller
Browse files Browse the repository at this point in the history
  • Loading branch information
austencollins committed Aug 3, 2015
1 parent 2248bbd commit bc91fba
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 17 deletions.
4 changes: 4 additions & 0 deletions api/users/show/event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"body": {},
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiJ1XzU1ZGZhNmMwLTNhMGItMTFlNS1hODA3LTI1NDU1ZjNiOTFiZSIsImlhdCI6MTQzODYyNTYwNywiZXhwIjoxNDM5MjMwNDA3LCJpc3MiOiJKQVdTIn0.hfNsHLzKJfWYHQ-I_8N2MDD5y0vuLW-Z0Iy15DM8psk"
}
27 changes: 27 additions & 0 deletions api/users/show/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* API: Users: Show
*/

// Dependencies
var ControllerIncoming = require('jaws-lib').controllers.Incoming;

// Function
exports.handler = function(event, context) {


console.time("Lambda Duration");
console.log("Event: ", event);


// Process Incoming Request
ControllerIncoming.process(event, context, function(event, context) {

/**
* Return
*/

console.timeEnd("Lambda Duration");
return context.succeed(event.req.user);

});
};
13 changes: 13 additions & 0 deletions api/users/show/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "servantapi",
"version": "0.0.1",
"description": "",
"main": "index.js",
"private": true,
"scripts": {
"test": "test"
},
"author": "Austen Collins <[email protected]>",
"license": "ISC",
"dependencies": {}
}
4 changes: 2 additions & 2 deletions api/users/signin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

// Dependencies
var AppModelsUser = require('jaws-lib').models.User;
var ModelUser = require('jaws-lib').models.User;

// Function
exports.handler = function(event, context) {
Expand All @@ -13,7 +13,7 @@ exports.handler = function(event, context) {
console.log("Event: ", event);


AppModelsUser.signIn(event.body, function(error, json_web_token) {
ModelUser.signIn(event.body, function(error, json_web_token) {

if (error) return context.fail(error);

Expand Down
4 changes: 2 additions & 2 deletions api/users/signup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

// Dependencies
var AppModelsUser = require('jaws-lib').models.User;
var ModelUser = require('jaws-lib').models.User;

// Function
exports.handler = function(event, context) {
Expand All @@ -13,7 +13,7 @@ exports.handler = function(event, context) {
console.log("Event: ", event);


AppModelsUser.signUp(event.body, function(error, json_web_token) {
ModelUser.signUp(event.body, function(error, json_web_token) {

if (error) return context.fail(error);

Expand Down
47 changes: 36 additions & 11 deletions lib/controllers/controller_incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
// Dependencies
var Config = require('../config/config');
var Utilities = require('../utilities/utilities');
var AppUser = require('../models/model_user');
var ModelUser = require('../models/model_user');

var jwt = require('jsonwebtoken');
var moment = require('moment');
var _ = require('lodash');


module.exports = new Incoming();


function Incoming() {}


Expand All @@ -23,23 +26,22 @@ function Incoming() {}
* - Process Incoming Request
*/

Incoming.prototype.process = function(event, callback) {
Incoming.prototype.process = function(event, context, callback) {


// Defaults
var _this = this;



/**
* Validate
*/

// Validate Access Token
if (!event.access_token) return callback({
if (!event.access_token) return context.fail({
status: 400,
message: 'Missing Access Token'
}, null);
});



Expand All @@ -48,28 +50,51 @@ Incoming.prototype.process = function(event, callback) {
*/

try {
var user_data = jwt.verify(event.access_token, '123');
var user_token = jwt.verify(event.access_token, Config.jwt.secret);
} catch (error) {
return callback({
return context.fail({
status: 401,
message: 'Invalid or expired access token'
}, null);
}

// Check Expiration, If Any
if (user_token.exp && user_token.exp < moment().unix()) return context.fail({
status: 401,
message: 'Expired access token'
}, null);

// Check Issuer
if (user_token.iss !== Config.jwt.issuer) return context.fail({
status: 401,
message: 'Invalid access token'
}, null);

// Check User ID
if (!user_token.uid) return context.fail({
status: 401,
message: 'Invalid access token'
}, null);



/**
* Pre-load User
*/

AppUser.showByID(user_data.user_id, function(error, user) {
ModelUser.showByID(user_token.uid, function(error, user) {

if (error) return context.fail(error);

if (error) return callback(error, null);
if (!user) return context.fail({
status: 404,
message: 'User not found'
});


/**
* Prepare Event Data
* Prepare Request Data
* - Attach relevant data to event.req
*/

event.req = {
Expand All @@ -79,7 +104,7 @@ Incoming.prototype.process = function(event, callback) {
};

// Return
return callback(null, event);
return callback(event, context);

});
}
3 changes: 1 addition & 2 deletions lib/models/model_user.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,14 @@ User.prototype.signIn = function(data, callback) {
/**
* Create JSON Web Token & Return
*/
console.log(Config)

var token = jwt.sign({
uid: user._id
}, Config.jwt.secret, {
issuer: Config.jwt.issuer,
expiresInSeconds: Config.jwt.expires_in_seconds
});

console.log(jwt.decode(token));

return callback(null, {
jwt: token
Expand Down

0 comments on commit bc91fba

Please sign in to comment.