# 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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dhavaln.gitbook.io/serverless-docs/congnito-configs/hook-allow-only-company-or-whitelisted.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
