Skip to content

Commit ed71523

Browse files
committed
fix: return HTTP 401 for unauthorized mutations and handle expired tokens
1 parent 14713ac commit ed71523

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/auth/middleware.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ async function validateTokenAndExtractUser (req: Request): Promise<CustomContext
4545
}
4646
} catch (e) {
4747
logger.error(`Can't verify JWT token ${e.toString() as string}`)
48-
throw new Error("Unauthorized. Can't verify JWT token")
48+
// Return empty user instead of throwing - allows public queries to work
49+
// Mutations will be blocked by graphql-shield permissions
50+
return { user: EMTPY_USER }
4951
}
5052
}
5153

src/auth/permissions.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { GraphQLError } from 'graphql'
12
import { allow, and, or, shield } from 'graphql-shield'
23
import { isEditor, isMediaOwner, isOwner, isUserAdmin, isValidEmail } from './rules.js'
34

@@ -24,7 +25,13 @@ const permissions = shield({
2425
},
2526
{
2627
allowExternalErrors: true,
27-
fallbackRule: allow
28+
fallbackRule: allow,
29+
fallbackError: new GraphQLError('Not Authorised!', {
30+
extensions: {
31+
code: 'FORBIDDEN',
32+
http: { status: 401 }
33+
}
34+
})
2835
})
2936

3037
export default permissions

0 commit comments

Comments
 (0)