Skip to content

Commit

Permalink
Add userId in GraphQL context after authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
MrOrz committed Apr 4, 2020
1 parent 5295edb commit 97b491d
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/graphql/__tests__/context.js
Expand Up @@ -34,6 +34,7 @@ it('Returns user context', async () => {
`(
{},
{
userId: 'U12345678',
userContext: {
state: 'CHOOSING_ARTICLE',
issuedAt: 1586013070089,
Expand Down
3 changes: 3 additions & 0 deletions src/graphql/__tests__/index.js
Expand Up @@ -14,6 +14,7 @@ describe('getContext', () => {
expect(context).toMatchInlineSnapshot(`
Object {
"userContext": null,
"userId": "",
}
`);
});
Expand All @@ -38,6 +39,7 @@ describe('getContext', () => {
expect(context).toMatchInlineSnapshot(`
Object {
"userContext": null,
"userId": "user1",
}
`);
});
Expand Down Expand Up @@ -66,6 +68,7 @@ describe('getContext', () => {
"foo": "bar",
"nonce": "correctpass",
},
"userId": "user1",
}
`);
});
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/directives/auth.js
Expand Up @@ -11,7 +11,7 @@ class AuthDirective extends SchemaDirectiveVisitor {
field.resolve = (...args) => {
const [, , context] = args;

if (!context.userContext) {
if (!context.userId || !context.userContext) {
throw new AuthenticationError('Invalid authentication header');
}

Expand Down
1 change: 1 addition & 0 deletions src/graphql/index.js
Expand Up @@ -46,6 +46,7 @@ export async function getContext({ ctx: { req } }) {
}

return {
userId,
userContext,
};
}
Expand Down

0 comments on commit 97b491d

Please sign in to comment.