Hellot guys I’m currently working on my authorizer lambda. But I can not wrap my head why it returns 500 error with empty msg: “” from my midleware, when I return response code and status message.
I have the following setup,
I have the middlewares
import middy from '@middy/core';
import httpHeadeNormalizer from '@middy/http-header-normalizer''
const authMiddleware = (hander) => {
return middy(handler).use(httpHeaderNormalizer())use(loadCreds())
}
const loadCreds = () => {
return {
before: async () => {
try {
const token = event.headers.authorization.replace(/[Bb]earer/, '');
try {
const decoded = jwt.verify(token, 'secret');
} catch (err) {
return {
statusCode: 401,
body: 'Unauthorized',
}
}
}
}
}
and my authorizer lambda
export const authHandler = authMiddleware(
async (event) => {
const requestContext: { accountId, stage, apiId}, methodArn = event;
const region = 'us-east-1';
const policy = new AuthPolicy(accountId, { apiId, region, stage});
const token = event.headers.authorization.replace(/[Bb]earer/, '');
try {
const decoded = jwt.verify(token, 'secret');
} catch(err) {
policy.denyAllMethods('Token expired');
return policy.build()
}
})
The problem is happening in my loadCreds middleware, the token expires
and I return the following
{
statusCode: 401,
body: 'Unauthorized',
}
But it is not working, the lambda always return 500 status code with empty body. If I use policy then its working fine, but it is returning code 403 instead of 401. And I wold like to send a custom code and custom response message. Is that possible?