# Prevent user creation (with hook?) if user is not whitelisted with rule

**URL:** <https://community.auth0.com/t/prevent-user-creation-with-hook-if-user-is-not-whitelisted-with-rule/62000>\
**Category:** Get Help\
**Tags:** rules, hooks, login, auth0\
**Created:** [April 30, 2021, 9:56am UTC](https://community.auth0.com/t/prevent-user-creation-with-hook-if-user-is-not-whitelisted-with-rule/62000 "2021-04-30T09:56:36Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![stephanie.chamblee](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/stephanie.chamblee/32/26087_2.png) [@stephanie.chamblee](https://community.auth0.com/u/stephanie.chamblee)\
**Post date:** [April 30, 2021, 12:32pm UTC](https://community.auth0.com/t/prevent-user-creation-with-hook-if-user-is-not-whitelisted-with-rule/62000/5 "2021-04-30T12:32:03Z")

</div>

Oh, I see the Google mention now! 🙃

Yes, only rules will cover all connections.

Usually, you’d just throw an authorization error like you’ve already mentioned. The user would be created, though. Here’s how you could delete the user:

1. Create a M2M application:

 ![Screen Shot 2021-04-30 at 8.01.22 AM](https://global.discourse-cdn.com/auth0/original/3X/4/9/491b4ecff6dd67bb8878277b69493755e6ec9e05.png)

1. Authorize it to use the Managment API and allow it to use the `delete:users` scope:  

2. Add the M2M app’s client ID and client Secret as secret values to use in the rule:  

3. Create the rule (this is allowing certain domains, but you can adjust the `userHasAccess` criteria for emails):

```auto
async function emailDomainWhitelist(user, context, callback) {
  const axios = require('axios@0.19.2');

  const whitelist = ['example.com', 'example.org']; //authorized domains
  const userHasAccess = whitelist.some(
      function (domain) {
        const emailSplit = user.email.split('@');
        return emailSplit[emailSplit.length - 1].toLowerCase() === domain;
      }
  );

  if (userHasAccess) {
    return callback(null, user, context);
  }

  const options = {
    method: 'POST',
    url: `https://${auth0.domain}/oauth/token`,
    headers: {
      'content-type': 'application/json'
    },
    data: {
      "client_id": configuration.DELETE_USERS_CLIENT_ID,
      "client_secret": configuration.DELETE_USERS_CLIENT_SECRET,
      "audience": `https://${auth0.domain}/api/v2/`,
      "grant_type":"client_credentials"
    }
  };

  try {
    const tokenResponse = await axios(options);
    const accessToken = tokenResponse.data.access_token;
    const userId = encodeURIComponent(user.user_id);
    const deleteUserOptions = {
      method: 'DELETE',
      url: `https://${auth0.domain}/api/v2/users/${userId}`,
      headers: { Authorization: `Bearer ${accessToken}` }
    };
    await axios(deleteUserOptions);
    return callback(new UnauthorizedError('Access denied.'));
  } catch (err) {
    // handle error
    console.log(err);
    return callback(new UnauthorizedError('Access denied.'));
  }
}

```

---

_[View the full topic](https://community.auth0.com/t/prevent-user-creation-with-hook-if-user-is-not-whitelisted-with-rule/62000)._
