Skip to content

How to add Discord login to your website

OAuth2 in five steps: app, redirect, authorize link, code exchange, user lookup.

Updated November 19, 2026

"Login with Discord" is OAuth2. Your site sends the visitor to Discord, they approve your app on Discord's screen, and Discord sends them back with a code your server trades for a token. You never see their password. Here is the whole flow, following Discord's developer documentation.

1. Create the application

  1. Open the Discord Developer Portal and create a New Application. Its name and icon are what users see on the consent screen.
  2. Under OAuth2, copy the Client ID and reset the Client Secret. Store the secret on your server only.
  3. Add a Redirect, for example https://yoursite.com/auth/discord/callback. It must match exactly what you send later.

2. Send the user to Discord

Your Login button links to the authorize URL with these parameters:

https://discord.com/oauth2/authorize
  ?response_type=code
  &client_id=YOUR_CLIENT_ID
  &scope=identify
  &state=RANDOM_VALUE_YOU_STORED
  &redirect_uri=https%3A%2F%2Fyoursite.com%2Fauth%2Fdiscord%2Fcallback

state is a random value you save in the user's session. When Discord sends them back, check it matches. That stops other sites from logging people in to your site with their own codes.

3. Pick scopes carefully

ScopeWhat it allowsAsk for it when
identify/users/@me without the emailAlways, for login
emailAdds the email to /users/@meYou need to contact them
guildsBasic info about every server they are inYou check membership of your server
guilds.members.readTheir member info in one serverYou check roles, for example subscriber roles
guilds.joinAdding them to a serverYou add users to your server, with your own bot in it

4. Exchange the code on your server

Discord redirects to your callback with code and state. Check the state, then POST form-encoded data to the token endpoint:

POST https://discord.com/api/oauth2/token
Content-Type: application/x-www-form-urlencoded
Authorization: Basic base64(CLIENT_ID:CLIENT_SECRET)

grant_type=authorization_code
&code=CODE_FROM_QUERY
&redirect_uri=https://yoursite.com/auth/discord/callback

You get back an access token, a refresh token, expires_in and the granted scope.

5. Read the user and start a session

GET https://discord.com/api/users/@me
Authorization: Bearer ACCESS_TOKEN

Use the returned id as the user's key in your database; usernames can change. Create your own session cookie and throw away the access token if you do not need to call Discord again. If you do, store the refresh token server-side and refresh with grant_type=refresh_token when it expires.

Logging out and cleaning up

Clearing your session logs the user out of your site. Revoking at https://discord.com/api/oauth2/token/revoke also removes your app's access, and Discord revokes every token tied to that authorization at once. Users can remove your app themselves in User Settings > Authorized Apps.

Security checklist

  • Use HTTPS for the redirect URL and every page in the flow.
  • Always send and check state. Reject the callback if it is missing or different.
  • Keep the client secret and refresh tokens on the server. Rotate the secret in the Developer Portal if it ever leaks.
  • Do not put the access token in a URL or in browser storage.
  • Only list exact redirect URLs in the portal. A broad list lets codes be sent somewhere you did not intend.
  • Treat the Discord user ID as the stable key. Display names and usernames can change at any time.

If what you want is verified server members

Many sites add Discord login for one reason: to get people into their server and keep a way back to them. That is the flow VaultCord already runs. Members verify on a branded page with captcha, approve the app on Discord's screen, get a role, and can be pulled into a new server if yours is deleted. No backend to write. On Plus, there is also a developer API to pull members with code. Try the Free plan, put a widget on your site, and check your link preview.

Questions

How do I add Discord login to my website?

Create an application in the Discord Developer Portal, add a redirect URL, send users to Discord's authorize URL with response_type=code and scope=identify, then exchange the code on your server and call /users/@me.

Which scopes do I need for Login with Discord?

identify is enough to know who the user is. Add email only if you need their email address. Ask for as little as possible; users see every scope on the consent screen.

Can I add users to my Discord server when they log in?

Yes, with the guilds.join scope and your own bot in that server. It adds the user to the server you choose, so say so clearly on your site.

Where do I keep the client secret?

Only on your server. The code exchange must happen in your backend, never in browser JavaScript.

How do I log a user out?

Clear your own session. To also remove your app's access, revoke the token at Discord's token revoke endpoint; that revokes every token for that authorization.