User Delete in auth0

I want to perform user deletion process in “onExecutePreUserRegistration.”
I’m having trouble with it.

const auth0 = require("auth0");
exports.onExecutePreUserRegistration = async (event, api) => {

  const managementClient = auth0.ManagementClient;
  const management = new managementClient({
      domain: event.secrets.domain,
      clientId: event.secrets.clientId,
      clientSecret: event.secrets.clientSecret,
  });

 userId = 'test1234567890';
  management.users.delete({ id: userId }, function (err) {
  	if (err) {
      console.log(err);
    }
  });

};
err
{
  init: { method: 'DELETE', headers: {}, body: undefined, agent: undefined },
  context: { path: '/users/test1234567890', method: 'DELETE' }
}

Could you please provide instructions on how to delete a user?

Thank you for posting @i-am-sato !

I’m looking into our documentation on the Pre User Registration flow, and I can see that those Actions run before the user is created on a database. The user doesn’t exist yet so it can not be deleted.

You can reject the user registration with the api.access.deny method by running an action analogically to the one below:

exports.onExecutePreUserRegistration = async (event, api) => {

  if (event.request.geoip.continentCode === "NA") {

    // localize the error message 
    const LOCALIZED_MESSAGES = {
      en: 'You are not allowed to register.',
      es: 'No tienes permitido registrarte.'
    };

    const userMessage = LOCALIZED_MESSAGES[event.request.language] || LOCALIZED_MESSAGES['en'];
    api.access.deny('no_signups_from_north_america', userMessage);
  }
};

Please let me if you have any other questions on this topic!

1 Like

Thank you for your response.

I apologize for the lack of clarity in my explanation.

If the same email address as mine is already registered, I would like to delete that user.
There are other conditions as well, but this is a measure to delete data that has become garbage.

I tried changing the process a bit, but the error still persists.

const auth0 = require("auth0");
exports.onExecutePreUserRegistration = async (event, api) => {

  const managementClient = auth0.ManagementClient;
  const management = new managementClient({
      domain: event.secrets.domain,
      clientId: event.secrets.clientId,
      clientSecret: event.secrets.clientSecret,
      scope: `delete:users delete:current_user`,
  });
  
  let userId = 'auth0|xxxxxxxxxxxxxxxxxxxx'; // Prepare an ID that exists.
  await await management.users.delete({ id:userId }, function (err) {
    if (err) {
      console.log(err);
    }
  });
};

ERROR

{
  "message": "Insufficient scope, expected any of: delete:users,delete:current_user",
  "name": "ManagementApiError",
  "stack": "ManagementApiError: Insufficient scope, expected any of: delete:users,delete:current_user\n    at UsersManager.parseError (/data/layers/layers-sB3e/sB3eAYbpTrt8QuVtIrpeZvW2ltBQ92kMEYe62vu_j-c/node_modules/auth0/dist/cjs/management/management-client.js:33:16)\n    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\n    at async UsersManager.request (/data/layers/layers-sB3e/sB3eAYbpTrt8QuVtIrpeZvW2ltBQ92kMEYe62vu_j-c/node_modules/auth0/dist/cjs/lib/runtime.js:119:23)\n    at async UsersManager.delete (/data/layers/layers-sB3e/sB3eAYbpTrt8QuVtIrpeZvW2ltBQ92kMEYe62vu_j-c/node_modules/auth0/dist/cjs/management/__generated/managers/users-manager.js:152:26)\n    at async exports.onExecutePreUserRegistration (/data/io/node18-actions/d0da7aea-41aa-43bd-a75c-fadde77fdd42/webtask.js:14:9)\n    at async /data/io/83d1a7565435f5e12c048147b30043a0d29de496.js:5:923"
}

LOG

{
  init: { method: 'DELETE', headers: {}, body: undefined, agent: undefined },
  context: { path: '/users/auth0%7Cxxxxxxxxxxxxxxxxxxxx', method: 'DELETE' }
}

We thought there was a mistake in the program, but the cause was different.

It was just that the Permissions setting was missing in Create Application.

I added checks to the necessary Permissions and it works as expected.

Thank you very much.

Thank you so much for sharing @i-am-sato ! I will go ahead and mark your last reply as a solution.

PS: I just noticed you have two "await keywords in your code snippet where you initiate a delete method :+1:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.