How to pass JWT to lambda function

I’m trying to pass an Identity instance’s JWT token (detailed here https://docs.netlify.com/functions/functions-and-identity/#access-identity-info-via-clientcontext) to a lambda function so I can take advantage of the clientContext.user object. I am using the following to code client-side:

netlifyIdentity.currentUser().jwt().then((token) => {
  let settings = {
    async: true,
    url: "https://" + window.location.host + "/.netlify/functions/getusers",
    method: "POST",
    data: netlifyIdentity.currentUser().user_metadata.id,
    dataType: "json",
    headers: {
      "cache-control": "no-cache",
      Authorization: "Bearer " + token,
    },
    complete: function (response) {
      if (response.responseJSON) {
        cb(response.responseJSON);
      }
    }
  }

  $.ajax(settings);
});

And here is the first part of the lambda function code:

export async function handler(event, context) {
  const { identity, user } = context.clientContext;
  console.log(user);
  const userID = event.body;
  const userUrl = `${identity.url}/admin/users/{${userID}}`;
  const adminAuthHeader = "Bearer " + identity.token;

  try {
    return fetch(userUrl, {
      method: "GET",
      headers: { Authorization: adminAuthHeader }
  })

When calling the function using the Identity’s JWT, a 500 Error is returned, and the function logs show no record of access. However if I remove the token, or purposely give it an invalid token, the function is invoked as normal (the clientContext.user object is expectedly undefined). I have verified this behavior through my own code and Postman. What is the proper way to pass a JWT to a lambda function?

Hi there, I think the issue with your particular case was the size of the data in your JWT was too big. JWT’s have to be sent as cookies, which means they need to be smaller than 4KB. While you can put data in JWT’s, you have to be careful how much data you pack in since they aren’t a database. Let us know if this wasn’t actually the solution to the issue you’re having or if you have any other questions!