Oh, I am talking about the serverless function that you’d write to update the user role, like this one here:
You’d have to set the role in this function using one of the admin methods of GoTrueJS. Once it’s set, you’d have to return the user back to client. Here’s an example:
return fetch(`${identity.url}/admin/users/${payload.netlifyId}`, {
method: 'put',
body: JSON.stringify({
app_metadata: {
roles: [payload.role]
}
}),
headers: {
Authorization: `Bearer ${identity.token}`
}
}).then(response => {
return response.json()
}).then(updatedUser => {
return {
statusCode: 200,
body: JSON.stringify(updatedUser)
}
})
Taken from:
So, according to the discussion we had, the code was working fine for a small number of users. But, @carvill had around 14k users where this code was failing. Chances are, the size of the users array was getting more than what a serverless function could handle. Thus, the solution was to store the identity user ID in an external database which you can query. Then, you just have to pass the ID with the payload and in the function, change role like:
const fetch = require('node-fetch')
exports.ha…
updatedUser
is the object you’d get here:
1 Like