# Build a custom solana faucet using Next.js and solana/web3.js - Part 2

## Introduction

So in [part 1](https://sanjayrajesh.hashnode.dev/custom-solana-faucet-using-nextjs-part-1) of the blog, we implemented airdrop functionality for our faucet. Now lets improve faucet security by adding a [reCAPTCHA](https://en.wikipedia.org/wiki/ReCAPTCHA) to protect against spam and abuse from bots. We'll also deploy our faucet to Vercel to make it available to 24/7 for any thirsty fellow glass chewers.

## Implementing reCAPTCHA 

### npm package

We'll make use of npm package [react-google-recaptcha](https://www.npmjs.com/package/react-google-recaptcha).

Make sure to install it
``` npm i react-google-recaptcha ```

We're given component called ``` ReCAPTCHA```
Which takes in 2 props, ```sitekey``` and ```onChange``` function. 

### Generating sitekey

Start by [generating a sitekey](https://www.google.com/recaptcha/admin/create). Fill in all the details as needed . Domains field keep track of where the captch is valid. For now just add ```localhost```. We'll come back to this when we deploy our app.
Then submit.


![Screenshot from 2022-03-31 12-58-12.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1648711717082/HGVCGMP-6.png)

You'll recieve a ```sitekey``` and a ```secretkey```. Keep this safe.
![Screenshot from 2022-03-31 12-59-47.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1648712019861/ENfUskwwK.png)

We'll have to pass this ```sitekey``` to  ``` ReCAPTCHA``` component. 

### Setting up environment variables in Next.js

Although we can do it directly, let's create an environment variable for security and best practice as we don't want sensitive information to be pushed to Repository.

Back in our app, at root folder, create a file called ```.env.local``` and add your ```sitekey```.
> *.env.local*


```
SITE_KEY= < your site key >
```
Next.js will automatically pick up ```SITE_KEY``` from ```.env.local``` file and we can access it with ```process.env.SITE_KEY```. There is however a catch as this only available on the server. To make it availabe on the client side, we should include this in js bundle.
Luckily, we have another file in our root directory called ```next.config.js```, we'll add it here to make the environment variable available on client side

```
/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  env: {
    SITE_KEY: process.env.SITE_KEY,
  },
};

module.exports = nextConfig;
```

> **NOTE: ** Since version 9.4, Next.js will make all environment variables that start with ```NEXT_PUBLIC_``` available to the client by default. So we can also use that approach.

### Implementing reCaptcha

Back in our ```Faucet``` component,  let's add ```ReCaptcha``` between the ```<Input>``` and ```<Button>```. We'll also create a state called ```isHuman``` to check is captcha was completed and set it to ```true``` inside our ```onChange``` function. 
Finally, we'll add a condition to enable our request airdrop button, only is address is valid and captcha is completed.
 

```
   ...

     import ReCAPTCHA from "react-google-recaptcha";

 ....

    const [isHuman, setIsHuman] = useState<boolean>(false);
    const siteKey: any = process.env.SITE_KEY;

...

function onChange(value: any) {
    console.log("Captcha value:", value);
    setIsHuman(true);
  }

...
     <Input
        placeholder="Enter solana wallet address"
        value={address}
        size="lg"
        width={"lg"}
        textAlign="center"
        onChange={(e: ChangeEvent<HTMLInputElement>) =>
          setAddress(e.target.value)
        }
        color={"blackAlpha.900"}
        backgroundColor="plum"
        _placeholder={{ color: "blackAlpha.700" }}
      />
      <ReCAPTCHA sitekey={siteKey} onChange={onChange} />
      <Button
        mt={20}
        disabled={!isValid || !isHuman}
        onClick={requestAirdrop}
        isLoading={loading}
      >
        Request airdrop
      </Button>

...
```
> **Note:**  Typescript might throw a type error when you import ReCAPTCHA from "react-google-recaptcha". Just npm install the recommended @types in the error message to solve the error. 

## Deploying to Vercel 🚀

Finally it's time to deploy our app. Make sure you push you code to [Github](https://github.com) before hand. Create an account in [Vercel](https://vercel.com/dashboard?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app) and come to the [Dashboard](https://vercel.com/dashboard?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app). Click on **+ New Project**



![Screenshot from 2022-03-31 13-35-23.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1648713952397/b60IFMn5S.png)

Import the GitHub repository.


![Screenshot from 2022-03-31 13-36-41.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1648714027927/hBpbK8GBM.png)

Then add our environment variable viz ```SITE_KEY```. Click **Add**  and then **Deploy**

Almost done! In a few minutes, your deployment will be successful, but if you open the app, you'll see the captcha throwing an Error. We'll have to add the domain to our reCAPTCHA domain list.


![Screenshot from 2022-03-31 13-39-48.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1648714241237/05hTxrtjK.png)

Copy the app domain from Vercel (or even add your own custom domain)


![Screenshot from 2022-03-31 13-41-16.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1648714417794/Wffl0zyNV.png)

 And add it to the domain list. Save it. Back and reload the app and Voila !! 🎉

🎉 🎉 Finally our faucet is live !! 🎉🎉. For any thirsty fellow glass chewers to get some SOL and continue building.

🚀 Checkout mine here: 
https://stardust-solana-faucet.vercel.app/

🧑‍💻 You can find the code here:
https://github.com/sanjayrjs16/solana-dev-faucet

📺 Video tutorial of this article as a YouTube playlist  :
https://www.youtube.com/watch?v=SNEyD8Kn9Xk&list=PLHPQWp2x-x8DpUDdDLCeC19Y0I0_i-9N8

Hope you enjoyed this article. 🙂 
Would love to see what you guys build from this, let me know your suggestions and feedback below. 

**Thanks for reading, keep on BUIDLING ** 🙂

