Netlify site: chromictm.netlify.app
Tested locally using Netlify CLI
Hi!
So I’m trying to update Netlify Identity roles for a user on login using a netlify function:
exports.handler = async function (event, context) {
const { identity, user } = context.clientContext;
return fetch(`${identity.url}/admin/users/${user.sub}`, {
method: 'put',
body: JSON.stringify({
app_metadata: {
roles: ["user"]
}
}),
headers: {
Authorization: `Bearer ${identity.token}`
}
}).then(response => {
return response.json()
}).then(updatedUser => {
return {
statusCode: 200,
body: JSON.stringify(updatedUser)
}
})
};
Running the function:
netlifyIdentity.on('login', user => {
fetch("/.netlify/functions/test-identity", {
headers: {
'content-type': 'application/json',
Authorization: `Bearer ${user.token.access_token}`
}
}).then(response => {
if (response.ok) {
return response.json()
} else {
throw response.statusText
}
}).then(data => {
console.log(data)
}).catch(error => {
console.log(error)
})
});
When I try to run the program, I get a 401 response, saying: "This endpoint requires a Bearer token"
I know that the function is being run, and that a bearer token is sent in the header.
Any idea what i could have done wrong?
Thanks!