Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda authorizer response using async/await in Node.js

I'm trying to create a lambda authorizer on aws using node.js async/await instead of callbacks but there is no information on how to create the HTTP response returned to API Gateway. For example, if i return this :

{
  statusCode: 401
}

the API gateway doesn't seem to understand and return an error 403 to the client :

{
    "statusCode": 403,
    "error": "Forbidden",
    "message": "No principalId set on the Response"
}

Does anyone knows how to do what is described here : https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html but using async / await ?

Thanks in advance !

EDIT :

The way to return an error 401 is simply to throw an error like this : throw new Error("Unauthorized") And if the user is explicitly deny / allow, simply return the JSON policy.

like image 872
julient-monisnap Avatar asked Mar 11 '19 16:03

julient-monisnap


Video Answer


1 Answers

To return a 401 error you simply need to throw an error with "Unauthorized" as message, like this :

throw new Error("Unauthorized")

And if the user is explicitly deny / allow, simply return the JSON policy like you would do with callbacks.

like image 126
julient-monisnap Avatar answered Nov 15 '22 03:11

julient-monisnap