Skip to content

Commit

Permalink
fix: return proper unmasked 401 errors when unauthorized
Browse files Browse the repository at this point in the history
  • Loading branch information
darkbasic committed Nov 13, 2023
1 parent b784af6 commit 9cecbdd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
9 changes: 8 additions & 1 deletion modules/module-core/src/utils/authenticated-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { GraphQLError } from 'graphql';

export const authenticated =
<
CustomRoot,
Expand All @@ -18,7 +20,12 @@ export const authenticated =
return func(root, args, context, info);
}
if (!context.userId && !context.user) {
throw new Error('Unauthorized');
throw new GraphQLError('Unauthorized', {
extensions: {
code: 'UNAUTHENTICATED',
http: { status: 401 },
},
});
}
return func(root, args, context, info);
};
29 changes: 25 additions & 4 deletions modules/module-password/src/resolvers/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ import {
} from '@accounts/password';
import { AccountsServer, AccountsJsError } from '@accounts/server';
import { MutationResolvers } from '../models';
import { GraphQLError } from 'graphql';

export const Mutation: MutationResolvers = {
addEmail: async (_, { newEmail }, ctx) => {
const { user, injector } = ctx;

if (!(user && user.id)) {
throw new Error('Unauthorized');
throw new GraphQLError('Unauthorized', {
extensions: {
code: 'UNAUTHENTICATED',
http: { status: 401 },
},
});
}

const userId = user.id;
Expand All @@ -25,7 +31,12 @@ export const Mutation: MutationResolvers = {
const { user, injector } = ctx;

if (!(user && user.id)) {
throw new Error('Unauthorized');
throw new GraphQLError('Unauthorized', {
extensions: {
code: 'UNAUTHENTICATED',
http: { status: 401 },
},
});
}

const userId = user.id;
Expand Down Expand Up @@ -81,7 +92,12 @@ export const Mutation: MutationResolvers = {

// Make sure user is logged in
if (!(user && user.id)) {
throw new Error('Unauthorized');
throw new GraphQLError('Unauthorized', {
extensions: {
code: 'UNAUTHENTICATED',
http: { status: 401 },
},
});
}

const userId = user.id;
Expand All @@ -94,7 +110,12 @@ export const Mutation: MutationResolvers = {

// Make sure user is logged in
if (!(user && user.id)) {
throw new Error('Unauthorized');
throw new GraphQLError('Unauthorized', {
extensions: {
code: 'UNAUTHENTICATED',
http: { status: 401 },
},
});
}

const userId = user.id;
Expand Down
8 changes: 7 additions & 1 deletion modules/module-password/src/resolvers/query.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { GraphQLError } from 'graphql';
import { QueryResolvers } from '../models';
import { AccountsPassword } from '@accounts/password';

Expand All @@ -7,7 +8,12 @@ export const Query: QueryResolvers = {

// Make sure user is logged in
if (!(user && user.id)) {
throw new Error('Unauthorized');
throw new GraphQLError('Unauthorized', {
extensions: {
code: 'UNAUTHENTICATED',
http: { status: 401 },
},
});
}

// https://github.com/speakeasyjs/speakeasy/blob/master/index.js#L517
Expand Down

0 comments on commit 9cecbdd

Please sign in to comment.