Skip to content

Commit

Permalink
set up prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
mshick committed Apr 14, 2018
1 parent 3309c82 commit 78d9a56
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 32 deletions.
6 changes: 5 additions & 1 deletion .eslintrc
@@ -1,5 +1,9 @@
{
"extends": "parsley/esnext",
"extends": ["parsley/esnext", "prettier"],
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error"
},
"settings": {
"import/resolver": "node"
},
Expand Down
2 changes: 1 addition & 1 deletion .prettierignore
@@ -1 +1 @@
*
package.json
3 changes: 3 additions & 0 deletions .prettierrc
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
25 changes: 17 additions & 8 deletions lib/methods/perform-query.js
Expand Up @@ -15,6 +15,8 @@ const performQuery = server => {
jwtToken,
jwtSecret,
jwtAudiences,
jwtRole,
jwtVerifyOptions,
pgDefaultRole,
pgSettings
} = options;
Expand All @@ -25,18 +27,25 @@ const performQuery = server => {
jwtToken,
jwtSecret,
jwtAudiences,
jwtRole,
jwtVerifyOptions,
pgDefaultRole,
pgSettings
},
async context => {
return graphql(
schema,
query,
null,
{...context},
variables,
operationName
);
try {
const result = await graphql(
schema,
query,
null,
{...context},
variables,
operationName
);
return result;
} catch (error) {
throw error;
}
}
);
};
Expand Down
23 changes: 5 additions & 18 deletions lib/routes/graphql.js
Expand Up @@ -15,7 +15,7 @@ const shouldVerify = verify => request => {
);
};

const onPostAuth = server => {
const onPreAuth = server => {
const {settings} = server.plugins[pkg.name];
const {verifyOrigin} = settings.authentication;
const shouldVerifyOrigin = shouldVerify(verifyOrigin);
Expand Down Expand Up @@ -58,37 +58,24 @@ const onPreResponse = server => {

const getJwtToken = server => {
const {settings} = server.plugins[pkg.name];
const {cookieAuthentication, headerAuthentication, schemaOptions} = settings;
const {cookieAuthentication, headerAuthentication} = settings;

return async (request, h) => {
let token;

if (request.auth && request.auth.token) {
// If using hapi-auth-jwt2 allow it to take over
return request.auth.token;
}

if (cookieAuthentication && cookieAuthentication.name) {
token = request.state[cookieAuthentication.name];
return request.state[cookieAuthentication.name];
}

if (headerAuthentication) {
const {headerName, tokenType} = headerAuthentication;
const header = request.headers[headerName.toLowerCase()];
if (header && header.startsWith(tokenType)) {
const tokenTypeLength = tokenType.length;
token = header.substr(tokenTypeLength).trim();
}
}

if (token) {
const {jwtSecret} = schemaOptions || {};

try {
await jwtVerify(token, jwtSecret);
return token;
} catch (error) {
throw Boom.badRequest(error);
return header.substr(tokenTypeLength).trim();
}
}

Expand Down Expand Up @@ -124,7 +111,7 @@ const handler = server => {
};

module.exports = {
onPostAuth,
onPreAuth,
onPreResponse,
getJwtToken,
handler
Expand Down
7 changes: 4 additions & 3 deletions lib/routes/index.js
@@ -1,6 +1,6 @@
const Boom = require('boom');
const {
onPostAuth,
onPreAuth,
onPreResponse,
getJwtToken,
handler
Expand All @@ -20,7 +20,7 @@ const register = server => {

if (authentication.verifyOrigin !== 'never') {
ext.onPostAuth = {
method: onPostAuth(server)
method: onPreAuth(server)
};
}

Expand All @@ -32,7 +32,8 @@ const register = server => {

const pre = routeOptions.pre || [];

pre.push({
// Place first, in case user functions require
pre.unshift({
assign: 'jwtToken',
method: getJwtToken(server)
});
Expand Down
39 changes: 39 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -45,11 +45,14 @@
"dotenv": "^5.0.1",
"eslint": "^4.19.1",
"eslint-config-parsley": "^1.2.1",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-filenames": "^1.2.0",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-prettier": "^2.6.0",
"hapi": "^17.2.3",
"pg": "^7.4.1",
"pg-native": "^2.2.0"
"pg-native": "^2.2.0",
"prettier": "^1.12.0"
},
"peerDependencies": {
"hapi": "^17.0.0",
Expand Down

0 comments on commit 78d9a56

Please sign in to comment.