---
description: >-
  We are building an enterprise document management system. So we only want to
  allow only users who belong to our organization or whitelisted emails.
---

# Allow only Organization and Whitelisted users to Signup

We will use Cognito Pre-Signup Hook to validate the email address and accordingly allow users to continue.&#x20;

![Cognito Pre-signup Trigger](https://1642315733-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCam2LtfwWxgwwpbHcu%2F-MCepGdtHvKkw9moJAwA%2F-MCewY0A4fshAY-RcbUq%2Flambda-pre-sign-up-2.png?alt=media\&token=33d61998-1e53-42bc-bfa0-9e8c32490ac9)

Here is the Lambda code which does the validation.&#x20;

```
const whitelistedDomains = ['appgambit.com'];
const whitelistedEmails = [];

exports.handler = async (event, context, callback) => {    
    // Split the email address so we can compare domains
    const userEmail = event.request.userAttributes.email;
    const userDomain = userEmail.split("@")[1];
    
    console.log(`Validating domain ${userDomain} and email ${userEmail}`);
    if(whitelistedEmails.indexOf(userEmail) < 0){
        if (whitelistedDomains.indexOf(userDomain) < 0) {
            throw new Error('EMAIL_DOMAIN_ERR')
        }
    }

    // Return to Amazon Cognito
    return event;
};
```

You can configure your Cognito pool to use the above lambda function.&#x20;

![Cognito Pre sign-up Lambda Trigger](https://1642315733-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCam2LtfwWxgwwpbHcu%2F-MCepGdtHvKkw9moJAwA%2F-MCfBFVZjPG5J0stoNJL%2FScreen%20Shot%202020-07-20%20at%201.16.51%20PM.png?alt=media\&token=68bbb925-47f7-4eaf-90cc-5dc2971ad937)

Now if you try to use an email that is not in the whitelist, you should see an error message. I am returning an error code **`EMAIL_DOMAIN_ERR`** and showing the proper message on the UI side.

![](https://1642315733-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCam2LtfwWxgwwpbHcu%2F-MCepGdtHvKkw9moJAwA%2F-MCf2mBr7zv_3HRU96Ap%2FScreen%20Shot%202020-07-20%20at%2012.39.57%20PM.png?alt=media\&token=d178441c-dfd4-4d22-9313-f2f6b6f57d82)

{% hint style="info" %}
At this point, the whole application will only accept only whitelisted signups. So this is now ready to use and share with your users.
{% endhint %}
