Skip to content

Commit

Permalink
Merge pull request #57 from joe-re/inject-client-context-to-dev-server
Browse files Browse the repository at this point in the history
inject clientContext to served function on dev-server
  • Loading branch information
swyxio authored Jan 10, 2019
2 parents 632e4fd + a796c6e commit 852fda2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var expressLogging = require("express-logging");
var queryString = require("querystring");
var path = require("path");
var conf = require("./config");
var jwtDecode = require("jwt-decode")

function handleErr(err, response) {
response.statusCode = 500;
Expand Down Expand Up @@ -76,7 +77,17 @@ function createHandler(dir, static) {
};

var callback = createCallback(response);
var promise = handler.handler(lambdaRequest, {}, callback);
let clientContext = {}
if (request.headers['authorization']) {
const parts = request.headers['authorization'].split(' ')
if (parts.length === 2 && parts[0] === 'Bearer') {
clientContext = {
identity: { url: '', token: '' },
user: jwtDecode(parts[1])
}
}
}
var promise = handler.handler(lambdaRequest, { clientContext }, callback);
promiseCallback(promise, callback);
};
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"commander": "^2.17.1",
"express": "^4.16.3",
"express-logging": "^1.1.1",
"jwt-decode": "^2.2.0",
"toml": "^2.3.3",
"webpack": "^4.17.1",
"webpack-merge": "^4.1.4"
Expand Down
17 changes: 17 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2040,6 +2040,23 @@ json5@^0.5.0:
version "0.5.1"
resolved "https://registry.yarnpkg.com/json5/-/json5-0.5.1.tgz#1eade7acc012034ad84e2396767ead9fa5495821"

jsonify@~0.0.0:
version "0.0.0"
resolved "https://registry.yarnpkg.com/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73"

jsprim@^1.2.2:
version "1.4.1"
resolved "https://registry.yarnpkg.com/jsprim/-/jsprim-1.4.1.tgz#313e66bc1e5cc06e438bc1b7499c2e5c56acb6a2"
dependencies:
assert-plus "1.0.0"
extsprintf "1.3.0"
json-schema "0.2.3"
verror "1.10.0"

jwt-decode@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/jwt-decode/-/jwt-decode-2.2.0.tgz#7d86bd56679f58ce6a84704a657dd392bba81a79"

kind-of@^3.0.2, kind-of@^3.0.3, kind-of@^3.2.0:
version "3.2.2"
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.2.2.tgz#31ea21a734bab9bbb0f32466d893aea51e4a3c64"
Expand Down

0 comments on commit 852fda2

Please sign in to comment.