aws lambda authorizer function, jsonwebtoken error "Invalid Token", using Bearer before and splicing it

150 Views Asked by At

Im using Lambda Authorizer in AWS and JsonWebToken to pass the token in the headers in Api Gateway. This is my code.

export const handler = async (event) => {
  const secret = "sof-admin-token";
  let auth = "Deny";
  const bearer = event["adminToken"];
  if(bearer){
    console.log(bearer)
    const token = bearer.split(" ")[1];
    console.log(token)
    let decoded = await jwt.verify(token, secret); // <- HERE IS THE ERROR (verifying the token)
    console.log(decoded)
    const command = new GetCommand({
      TableName: "sof-admin-db",
      Key: {
        email: decoded.data
      }
    });
    const response = await docClient.send(command);
    if(response.Item && response.Item.role.indexOf("admin")>=0){
      auth = "Allow"
    }
  }
  console.log(auth)

After verify the token, I search in the database if is a admin user and then return the authorization or not.

This is the console.log and the token is correct.

INFO    eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2O…0MDR9.UiuZk8SkhVNYxOm1uo3HZhWqMQTsamA_F-6lk-eEh3M
ERROR   Invoke Error    {"errorType":"JsonWebTokenError","errorMessage":"invalid token"

I tried send the token without the bearer with the same results.

0

There are 0 best solutions below