How to add Discord Authentication to FoundryVTT
Tags: foundryvtt, discord, authentication
Published: 2021-06-27 19:33:03

## Steps

### 1) Downloading the discord auth files

1. Navigate to [github.com/mbround18/foundryvtt-discord-auth](https://github.com/mbround18/foundryvtt-discord-auth).
2. Click on the green code button:
![green code button](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0lg03egizv4yvgs33eko.png)
3. Click Download as zip 
![download as zip button](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10g8n5wcx31vq1kehr6e.png)

### 2) Extract the files to the foundry installation directory.

Either with a gui client such as 7zip or with terminal, extract the files to the `path/to/foundry/resources/app` directory. (where the `main.js` file is located)

Depending on what client you are using, it will ask to overwrite the `main.hbs` file. This is acceptable as the only modification to the original file is to load the discord script.

### 3) Create a new application on Discord

1. [Navigate to discord developers section](https://discord.com/developers/applications)
2. Log into your discord account if needed. 
3. Either modify an existing application or create a new application. 


### 4) Adding the Callback.

1. Click the OAuth2 tab under your application
![OAuth2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ahd10q4ee53f7nd5tmgi.png)
2. Click the Add Redirect button and add your foundrys url with `/join` at the end of it.
![redirect](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/akq33dr9ios42w2ufruj.png)
3. Click the save button.

### 5) Modify the `discord.js` script. 

1. While on your discord applications page, copy the application ID. 
![app id](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v8e1xj60rg0g71je98b9.png)
2. Now open and edit the file `public/scripts/discord.js`

If you are using the application for windows, mac, Linux, etc it might be under `path/to/foundry/resources/app/public/scripts/discord.js`

3. At the top of the file you will see the following configuration:

![config](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bs8dhcjodddusmk0chah.png)

4. Change the client ID to the one you have stored in your clipboard. 
5. Change the callback URL to match the one you saved in the redirects section on the discord developer portal.  
6. (Optional) You can choose to use `id, email` or just `id` if you want to simplify adding new users. 

### 6) Setting up the discord authentication in your browser. 

1. Navigate to your Foundry url.
2. Launch your world.
3. Log in as Gamemaster or your GM account. 
4. Navigate to the user management screen
5. You will now see several new fields, change the User Name to be your discord username. (If you click on your username in the discord application it will copy it to clipboard.)
6. Set the discord ID to be your Discord ID. [(You can get this by following this guide)](https://support.discord.com/hc/en-us/articles/206346498-Where-can-I-find-my-User-Server-Message-ID-)
7. Add the email for yourself.
8. Set the access key. 
9. (Optional) Repeat steps 5 through 8 for each user.
10. Click Save and Return

> If you opted to only use Discord ID or Email, your form will look different. 

![form example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d2mw8wcw7fbcuxrkh4l8.png)
 

### Log in with Discord

#### Logging in for the first time

1. Click the Log in with Discord button
2. Authorize the application.
3. Enter your access key.

![login](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/23d0xdm241xqzyhd3khf.png)
 
 

#### Returning to Foundry

1. Enter your access key.

![returning login](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xmkfo7ni51viz0l75kvy.png)