Updating users roles via Netlify Serverless function (When logged in as another user)

Okay, getting closer, but back to this error in the console log:

Response {type: "basic", url: "https://xx.netlify.app/.netlify/functions/updateRole/", redirected: false, status: 502, ok: false, …}
body: (...)
bodyUsed: true
headers: Headers {}
ok: false
redirected: false
status: 502
statusText: ""
type: "basic"
url: "https://xx.netlify.app/.netlify/functions/updateRole/"
__proto__: Response
{errorType: "Runtime.UserCodeSyntaxError", errorMessage: "SyntaxError: Cannot use import statement outside a module", trace: Array(10)}
errorMessage: "SyntaxError: Cannot use import statement outside a module"
errorType: "Runtime.UserCodeSyntaxError"
trace: (10) ["Runtime.UserCodeSyntaxError: SyntaxError: Cannot use import statement outside a module", "    at _loadUserApp (/var/runtime/UserFunction.js:98:13)", "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)", "    at Object.<anonymous> (/var/runtime/index.js:43:30)", "    at Module._compile (internal/modules/cjs/loader.js:999:30)", "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)", "    at Module.load (internal/modules/cjs/loader.js:863:32)", "    at Function.Module._load (internal/modules/cjs/loader.js:708:14)", "    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)", "    at internal/main/run_main_module.js:17:47"]
__proto__: Object

And this error in the serverless functions log:

11:02:25 AM: 2021-07-30T10:02:25.581Z	undefined	ERROR	Uncaught Exception 	{"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Cannot use import statement outside a module","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Cannot use import statement outside a module","    at _loadUserApp (/var/runtime/UserFunction.js:98:13)","    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)","    at Object.<anonymous> (/var/runtime/index.js:43:30)","    at Module._compile (internal/modules/cjs/loader.js:999:30)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)","    at Module.load (internal/modules/cjs/loader.js:863:32)","    at Function.Module._load (internal/modules/cjs/loader.js:708:14)","    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)","    at internal/main/run_main_module.js:17:47"]}
11:02:25 AM: 2021-07-30T10:02:25.794Z	undefined	ERROR	Uncaught Exception 	{"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Cannot use import statement outside a module","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Cannot use import statement outside a module","    at _loadUserApp (/var/runtime/UserFunction.js:98:13)","    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)","    at Object.<anonymous> (/var/runtime/index.js:43:30)","    at Module._compile (internal/modules/cjs/loader.js:999:30)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)","    at Module.load (internal/modules/cjs/loader.js:863:32)","    at Function.Module._load (internal/modules/cjs/loader.js:708:14)","    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)","    at internal/main/run_main_module.js:17:47"]}
11:02:25 AM: 7daffa72 Duration: 152.61 ms	Memory Usage: 14 MB	11:02:25 AM: Unknown application error occurred
Runtime.UserCodeSyntaxError

That is strange. Do you have a repo I could look at (and probably test it myself)?

Are we able to do this a bit more privately? rather than share credentials over the forum? does that option exist?

Yeah definitely, you could send the link to me in a PM (go to my user profile and send me a message), or if your repo is on GitHub, you can directly add me to it, my GitHub username: Hrishikesh-K.

Thanks pinged a message over!

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.handler =  async (event, context) => {
  const payload = JSON.parse(event.body)
  const identity = context.clientContext.identity
  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)
    }
  })
}
1 Like

Thanks again for your help on this, you have been outstanding!
I can confirm the reposne marked as “Solution” is working for me for over 14k users! :muscle: Big big thanks again to @hrishikesh