|
| 1 | +--- |
| 2 | +id: auth-discord |
| 3 | +title: "Setting up Discord Auth" |
| 4 | +description: Add Discord OAuth to your Supabase project |
| 5 | +--- |
| 6 | + |
| 7 | +import Tabs from '@theme/Tabs'; |
| 8 | +import TabItem from '@theme/TabItem'; |
| 9 | + |
| 10 | + |
| 11 | +To enable Discord Auth for your project, you need to set up a Discord Application and add the Application OAuth credentials to your Supabase Dashboard. |
| 12 | + |
| 13 | +## Overview |
| 14 | + |
| 15 | +Setting up Discord logins for your application consists of 3 parts: |
| 16 | + |
| 17 | +* Create and configure a Discord Application [Discord Developer Portal](https://discord.com/developers) |
| 18 | +* Add your Discord OAuth Consumer keys to your [Supabase Project](https://supabase.io) |
| 19 | +* Add the login code to your [Supabase JS Client App](https://github.com/supabase/supabase-js) |
| 20 | + |
| 21 | +## Steps |
| 22 | + |
| 23 | +### Access your Discord account |
| 24 | + |
| 25 | +- Go to [discord.com](https://discord.com/). |
| 26 | +- Click on `Login` at the top right to log in. |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +- Once logged in, go to [discord.com/developers](https://discord.com/developers). |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +### Find your callback URL |
| 36 | + |
| 37 | +In the next step you require a callback URL, which looks like this: |
| 38 | + |
| 39 | +`https://<project-ref>.supabase.co/auth/v1/callback` |
| 40 | + |
| 41 | +- Go to your [Supabase Project Dashboard](https://supabase.io). |
| 42 | +- Click on the `Settings` icon at the bottom of the left sidebar. |
| 43 | +- Click on `API` in the list. |
| 44 | +- Under Config / URL you'll find your API URL, you can click `Copy` to copy it to the clipboard. |
| 45 | +- Now just add `/auth/v1/callback` to the end of that to get your full `OAuth Redirect URI`. |
| 46 | + |
| 47 | +<video width="99%" muted playsInline controls="true"> |
| 48 | +<source src="/videos/api/api-url-and-key.mp4" type="video/mp4" muted playsInline /> |
| 49 | +</video> |
| 50 | + |
| 51 | +### Create a Discord Application |
| 52 | + |
| 53 | +- Click on `New Application` at the top right. |
| 54 | +- Enter the name of your application and click `Create`. |
| 55 | +- Click on `OAuth2` under `Settings` in the left side panel. |
| 56 | +- Click `Add Redirect` under `Redirects`. |
| 57 | +- Type or paste your `callback URL` into the `Redirects` box. |
| 58 | +- Click `Save Changes` at the bottom. |
| 59 | +- Copy your `Client ID` and `Client Secret` under `Client information`. |
| 60 | + |
| 61 | +### Add your Discord credentials into your Supabase Project |
| 62 | + |
| 63 | +- Go to your [Supabase Project Dashboard](https://supabase.io) |
| 64 | +- In the left sidebar, click the `Authentication` icon (near the top) |
| 65 | +- Click `Settings` from the list to go to the `Authentication Settings` page |
| 66 | +- Enter the final (hosted) URL of your app under `Site URL` (this is important) |
| 67 | +- Under `External OAuth Providers` turn `Discord Enabled` to ON |
| 68 | +- Enter your `client_id` and `client_secret` saved in the previous step |
| 69 | +- Click `Save` |
| 70 | + |
| 71 | +### Add login code to your client app |
| 72 | + |
| 73 | +The JavaScript client code is documented here: [Supabase OAuth Client Code](https://supabase.io/docs/reference/javascript/auth-signin#sign-in-using-third-party-providers) |
| 74 | + |
| 75 | +```js |
| 76 | +const { user, session, error } = await supabase.auth.signIn({ |
| 77 | + provider: 'discord' |
| 78 | +}) |
| 79 | +``` |
| 80 | + |
| 81 | +Add this function which you can call from a button, link, or UI element. |
| 82 | + |
| 83 | +```js |
| 84 | +function signInWithDiscord() { |
| 85 | + const { user, session, error } = await supabase.auth.signIn({ |
| 86 | + provider: 'discord' |
| 87 | + }); |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +To log out: |
| 92 | + |
| 93 | +```js |
| 94 | +function signout() { |
| 95 | + const { error } = await supabase.auth.signOut(); |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +## Resources |
| 100 | + |
| 101 | +* [Supabase Account - Free Tier OK](https://supabase.io) |
| 102 | +* [Supabase JS Client](https://github.com/supabase/supabase-js) |
| 103 | +* [Discord Account](https://discord.com) |
| 104 | +* [Discord Developer Portal](https://discord.com/developers) |
0 commit comments