# How to allow specific emails to login with auth0?

**URL:** <https://community.auth0.com/t/how-to-allow-specific-emails-to-login-with-auth0/96912>\
**Category:** Get Help\
**Tags:** passwordless-email-connection, connections\
**Created:** [December 21, 2022, 4:12pm UTC](https://community.auth0.com/t/how-to-allow-specific-emails-to-login-with-auth0/96912 "2022-12-21T16:12:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![maury](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/maury/32/33197_2.png) [@maury](https://community.auth0.com/u/maury)\
**Post date:** [December 21, 2022, 4:12pm UTC](https://community.auth0.com/t/how-to-allow-specific-emails-to-login-with-auth0/96912/1 "2022-12-21T16:12:47Z")

</div>

Hey 👋 , I just created a .NET API and followed the getting started guide. What I would like to add is allow specific people from my org to login with an specific email? Is that possible?

I have found this thread but the solution that gives the answer doesn’t show the email domain whitelist rule anymore: [How can I only allow specific people to sign up](https://community.auth0.com/t/how-can-i-only-allow-specific-people-to-sign-up/33152)

Cheers.

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [December 21, 2022, 4:53pm UTC](https://community.auth0.com/t/how-to-allow-specific-emails-to-login-with-auth0/96912/3 "2022-12-21T16:53:17Z")

</div>

Hi there @maury welcome to the community!

This rule exists as a template if you navigate to Auth Pipeline → Rules → Create - You’ll see it under Access Control specifically. It looks like this:

```auto
function emailDomainWhitelist(user, context, callback) {
  // Access should only be granted to verified users.
  if (!user.email || !user.email_verified) {
    return callback(new UnauthorizedError('Access denied.'));
  }

  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(new UnauthorizedError('Access denied.'));
  }

  return callback(null, user, context);
}

```

Is that what you are looking for? Let us know!

---

<div class="post-metadata">

**Author:** ![maury](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/maury/32/33197_2.png) [@maury](https://community.auth0.com/u/maury)\
**Post date:** [December 23, 2022, 7:33pm UTC](https://community.auth0.com/t/how-to-allow-specific-emails-to-login-with-auth0/96912/4 "2022-12-23T19:33:50Z")

</div>

Thank you @ty.frith !

What I’m trying to do is protect my API and don’t let anyone to request it, just certain people. Also, on the frontend side I would like to just use only email option to log in. Is that possible?

If so, what do you recommend me to achieve this?

Once again, thanks for taking your time to reply 😃

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [December 24, 2022, 12:09am UTC](https://community.auth0.com/t/how-to-allow-specific-emails-to-login-with-auth0/96912/5 "2022-12-24T00:09:25Z")

</div>

No problem, happy to help where I can!

> [@maury](#):
>
> What I’m trying to do is protect my API and don’t let anyone to request it, just certain people.

I’m not clear on whether you want anyone to be able to login, but then only a subset of users access an API, or only allow a subset of users to login/access the API. Typically, an application will use some sort of permissions for a subset of users to be able to access an API. Please see the following doc for a general outline of what this may look like:

> **[Single-Page Applications (SPA) with API](https://auth0.com/docs/get-started/architecture-scenarios/spa-api)**
>
> Explains the architecture scenario where a single-page application (SPA) talks to an API using OpenID Connect (OIDC), and the OAuth 2.0 Implicit Grant Flow, to authenticate users with Auth0.

> [@maury](#):
>
> Also, on the frontend side I would like to just use only email option to log in. Is that possible?

When you say only email to login, are you referring to a [passwordless](https://auth0.com/docs/authenticate/passwordless) solution or something else?

The more context you can provide about your use case the better!

---

<div class="post-metadata">

**Author:** ![maury](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/maury/32/33197_2.png) [@maury](https://community.auth0.com/u/maury)\
**Post date:** [December 26, 2022, 6:18pm UTC](https://community.auth0.com/t/how-to-allow-specific-emails-to-login-with-auth0/96912/6 "2022-12-26T18:18:14Z")

</div>

First of all, Merry Christmas @ty.frith 🎄😀.

> I’m not clear on whether you want anyone to be able to login, but then only a subset of users access an API, or only allow a subset of users to login/access the API. Typically, an application will use some sort of permissions for a subset of users to be able to access an API. Please see the following doc for a general outline of what this may look like:

Yeah, what I would like to do is only allow a subset of users to login/access the API. I’m creating a table with the specifics emails (i don’t know if its necessary at all, but I think so).

> When you say only email to login, are you referring to a passwordless solution or something else?

I didn’t even know that exists!

> The more context you can provide about your use case the better!

My bad, sry! 😣. I have an API in .NET and what I would like to do is protect the endpoints so that way specific people can fetch it. In the other hand (here its where it gets cloudly to me) I would like my front-app (here comes auth0, I think?) to have some auth(login option) when you enter it and the only access to pass through it is with a specific email (what I don’t really know is if I need to send some JWT through my back or not)

Sry if that doesn’t make sense.

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [March 11, 2023, 4:00pm UTC](https://community.auth0.com/t/how-to-allow-specific-emails-to-login-with-auth0/96912/7 "2023-03-11T16:00:52Z")

</div>

This topic was automatically closed after 2 days. New replies are no longer allowed.

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [August 19, 2024, 6:16pm UTC](https://community.auth0.com/t/how-to-allow-specific-emails-to-login-with-auth0/96912/8 "2024-08-19T18:16:16Z")

</div>

Re-visiting this as we comb through our backlog and hope this helps someone in the future 🔮

> [@maury](#):
>
> My bad, sry! 😣. I have an API in .NET and what I would like to do is protect the endpoints so that way specific people can fetch it. In the other hand (here its where it gets cloudly to me) I would like my front-app (here comes auth0, I think?) to have some auth(login option) when you enter it and the only access to pass through it is with a specific email (what I don’t really know is if I need to send some JWT through my back or not)

There are a couple of ways you can go about this - You could simply create an Action to “allow list” specific emails:

```auto
exports.onExecutePostLogin = async (event, api) => {
  // Define your allowlist of emails
  const allowlist = [
    'alloweduser1@example.com',
    'alloweduser2@example.com',
    'alloweduser3@example.com'
  ];

  // Check if the user's email is in the allowlist
  if (!allowlist.includes(event.user.email)) {
    // If the email is not in the allowlist, deny access
    api.access.deny('Access denied: Your email is not in the allowlist.');
  }
};

```

The 2nd and more scaleable approach is to implement [Role Based Access Control](https://auth0.com/docs/manage-users/access-control/rbac) - At a high level this includes assigning roles (and subsequently API permissions) to users. In your registered API settings, you will want to [enable RBAC](https://auth0.com/docs/get-started/apis/enable-role-based-access-control-for-apis) and most likely the option to include permissions in access tokens. When a user logs in they will receive an access token, and if permissions are included in the token these are what your API, after verifying the access token, will use to make authorization decisions. Alternatively or in addition to permissions, you can add roles to your user’s access tokens as well using a Post Login Action:

```auto
exports.onExecutePostLogin = async (event, api) => {
  // Get the user's roles from the event object
  const roles = event.authorization.roles;

  // Add the roles to the access token as a custom claim
  api.accessToken.setCustomClaim('https://your-domain.com/roles', roles);
};

```
