Working with Discord OAUTH2 in JS
Last Updated: August 14, 2022
Discord OAUTH2 is a system that allows users to login to an external website (such as a personal blog or
bot dashboard) with Discord. It shares limited information about the user, such as the ID, user tag, or
even email. The information Discord gives regarding the user must be specified in the Discord
developer portal and the user will typically be asked to allow Discord to share that limited data with
the external website while logging in.
OAUTH, also known as OAuth or Open Authentication, is a system that allows large websites to grant
third-party applications limited information on a user
when the user grants access to the third-party app/website. This allows users to sign in to many
websites accross the web by logging in with one website and granting access to the third-party
application. This system can be complex at
first, but overall it is worth the effort to implement. This example will use Node.js.
Setup in the Discord Developer
Portal
To start, you will need to create an application in the Discord Developer Portal. To do so, first login with your
regular Discord account then press "New Application" at the top right corner.
When you click
on the "New Application" button, a popup will open and it will prompt you for the application name
(and possibly team). Enter your application name in the designated space then click "Create" or
press Enter.
Next, click on the
OAuth2
tab.
Make a note of your Client ID
and Client Secret. They will be used later.
Click "Add
Redirect" then type http://localhost:80
into the box and
save your changes.
Then, navigate
to the URL Generator section under OAuth2, select "Identify" from the scopes box , and select your
redirect URL from the dropdown menu. You should see a link form in the output box labeled below
"Generated URL". Copy that link (either with CTRL/CMD C or with the copy button
next to it) and save it somewhere. It will come in handy.
npm i express url axios path body-parser node-fetch
oauth.js
file. For simplicity, I named it oauth.js, but you
are free to name it however you would like. First, we will want to "import", or "require" our
dependencies. To do so, enter the following code.node oauth.js
), you shouldn't get any errors. If you do get
any, you probably didn't install the dependencies correctly. Next, let's define our bot variables.
Remember when you got the Client ID and Client Secret? Those will come in handy now.
This tells the program your client info so the program can later make a request to the Discord API
with that info. Next, let's define some more things.
The above code creates a new express application and creates a function that will generate headers
for a Discord API request. Next, let's configure our app to be able to read and interpret inputs in
POST and GET requests.
req.body
will be the code OAuth2 gives the user's browser when redirecting the user. To
get the access token, we will have to make a request to the Discord API with certain parameters.
This is where the URL dependency comes in hand.
We can use it to construct the parameters we must send to the API.data_1
, then assign data to it. It will
give the basic info (client ID, client secret (yes, that's what they are used for in this case!)),
the grant type, redirect URI, scope, and code. The grant type is essentially what you want or need
from the API. In this case, we're looking for
the authorization token. The redirect URI is where the users was redirected. It should be the same
as the one you put in the developer portal. The scope is what kind of information you are looking
for on the user. In this case, we only selected
Identify. You can add more by selecting the appropriate ones in the developer portal and adding them
into the link and where the scope is appended to the data. If you add more, be sure to add a %20 in
between each scope. Finally, we have the code.
The code is the initial code the user got from being redirected from the login link. That's all the
data. Next thing to do is to make a request to the Discord API. We'll be using node-fetch initially.
We'll want to make a POST request. Replace
your app.post() function with this.data => {
), enter the following code.[DISCORD OAUTH LINK HERE]
.node oauth.js
in your console and navigate to
your localhost address. Click on "Login With Discord", login, wait a second
or two, and it should work!
To set up Discord OAUTH2 with Node.JS, first log into the Discord Developer portal. Next, navigate to the application you would like to set up OAUTH2 with, or create a new application. Then, navigate to the OAUTH2 tab and make a note of your Client ID and Client Secret. These will tell the Discord API which application is being used. Click “Add Redirect” then type your redirect URI. If you are hosting your own program, add a localhost domain. That is everything required from the Dev Portal. The next thing to do is install dependencies and set up your backend code. Finally, set up your front end HTML page and run the program. The code required can be found above or on GitHub.