"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
- Open the Discord Developer Portal and create a New Application. Its name and icon are what users see on the consent screen.
- Under OAuth2, copy the Client ID and reset the Client Secret. Store the secret on your server only.
- 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%2Fcallbackstate 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
| Scope | What it allows | Ask for it when |
|---|---|---|
identify | /users/@me without the email | Always, for login |
email | Adds the email to /users/@me | You need to contact them |
guilds | Basic info about every server they are in | You check membership of your server |
guilds.members.read | Their member info in one server | You check roles, for example subscriber roles |
guilds.join | Adding them to a server | You 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/callbackYou 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_TOKENUse 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.