Hi there,
I’ve built a small React app that uses a Netlify function to do an API call, and I am trying to get the user from the clientContext to be able to do some authentication checks there as well. I’m using Firebase authentication rather than Netlify Identity, and I suspect maybe that is causing this? Could you please take a look and help me understand what the issue could be?
Here is the code making the request:
const options = {
method: "POST",
headers: {
"Authorization": `Bearer ${auth.currentUser.accessToken}`,
"Content-Type": "application/json",
},
url: "/.netlify/functions/shorten",
data: JSON.stringify({
url: url_display.textContent,
}),
};
await axios(options).then((response) => {
if (response.status === 200) {
shorturl_display.textContent = response.data;
} else {
return Promise.reject(response.status);
}
})
And this results in a request with these headers - from this it does seem the bearer token is being sent
However the user does not appear in the clientContext. Here is what I get when I console log the context from the netlify function on the deployed site:
context {
callbackWaitsForEmptyEventLoop: [Getter/Setter],
succeed: [Function (anonymous)],
fail: [Function (anonymous)],
done: [Function (anonymous)],
functionVersion: '$LATEST',
functionName: 'a70be4afcf8d23464d94b2345facd8d0581b1aea579cc3cbae12ce9b1c30f1f5',
memoryLimitInMB: '1024',
logGroupName: '/aws/lambda/a70be4afcf8d23464d94b2345facd8d0581b1aea579cc3cbae12ce9b1c30f1f5',
logStreamName: '2022/01/14/[$LATEST]292c464ef9f24713ae6afc54a364f069',
clientContext: {
custom: {
netlify: 'xxx'
},
identity: {
url: 'https://deploy-preview-1--friendly-lichterman-a6a412.netlify.app/.netlify/identity',
token: 'xxx'
}
},
identity: undefined,
invokedFunctionArn: 'arn:aws:lambda:us-east-1:650953327525:function:a70be4afcf8d23464d94b2345facd8d0581b1aea579cc3cbae12ce9b1c30f1f5',
awsRequestId: '21359eb8-0f4e-45f4-b4d6-d2fc074b0438',
getRemainingTimeInMillis: [Function: getRemainingTimeInMillis]
}
At first, I was not the token here at all, until I enabled Identity on the site - now at least I can access the token that was sent, but user is still not coming through. Have I missed something or could this be because I’m trying to send a Firebase token?
In my local netlify dev setup, it seems to work fine, and user appears in the clientContext:
clientContext: {
identity: {
url: 'https://netlify-dev-locally-emulated-identity.netlify.com/.netlify/identity',
token: 'xxx'
},
user: {
name: 'xxx',
iss: 'https://securetoken.google.com/xxx',
aud: 'xxx',
auth_time: 1642140953,
user_id: 'RHDNMdwSt5RMyACnYSZqcdxxaX13',
sub: 'RHDNMdwSt5RMyACnYSZqcdxxaX13',
iat: 1642140953,
exp: 1642144553,
email: 'xxx',
email_verified: false,
firebase: [Object]
}
},
_stopped: false
}