I’m trying to pass an Identity instance’s JWT token (detailed here Functions and Identity | Netlify Docs) 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?