Sign in with Discord
For: an app developer whose users have Discord accounts, such as a game or a community.
What it does: social.discord adds a “Continue with Discord” button. Core finds or makes the user by the Discord user id.
Before you rely on this preset, sign in once with a real account.
Before you start
Section titled “Before you start”- A working social sign-in from Sign in with Google and Apple.
- An application in the Discord developer portal with OAuth2 on, for
social.discord.
- Add
https://<your api host>/auth/social/discord/callbackto the OAuth2 redirects of the Discord application. - Copy the client id and the client secret to your secret store.
- Set
social.discordwithclientIdandclientSecret. - Link to
/auth/social/discord/start?next=/homefrom the sign-in page.
import { createAuth, type MailPort, type StorageAdapter } from "@authtempo/core";
export function createDiscordAuth(deps: { storage: StorageAdapter; mail: MailPort; secret: string;}) { return createAuth({ baseURL: "https://api.example.com", appURL: "https://app.example.com", trustedOrigins: ["https://app.example.com"], secret: deps.secret, storage: deps.storage, mail: deps.mail, social: { // Read the secret from your secret store. The placeholders only build the config. discord: { clientId: "1234567890", clientSecret: "discord-client-secret" }, }, });}import { describe, expect, it } from "vitest";import { call, harness } from "../support/harness.ts";import { createDiscordAuth } from "./discord.ts";
describe("sign in with Discord", () => { it("sends the person to the provider with the scope and a PKCE challenge", async () => { const auth = createDiscordAuth(harness()); const res = await call(auth, "GET", "/auth/social/discord/start?next=/home"); expect(res.status).toBe(302); const to = new URL(res.headers.get("location") ?? ""); expect(to.origin + to.pathname).toBe("https://discord.com/oauth2/authorize"); expect(to.searchParams.get("scope")).toBe("identify email"); expect(to.searchParams.get("redirect_uri")).toBe( "https://api.example.com/auth/social/discord/callback", ); expect(to.searchParams.get("code_challenge_method")).toBe("S256"); });});How it behaves
Section titled “How it behaves”- The scope is
identify email. Core reads the profile fromGET /users/@me. - The subject is the user
id, the key that Better Auth keeps. A Better Authaccountrow of the providerdiscordlinks with no change. - An address counts as verified only with
verified: truein the profile. An unverified address links no user that exists. - The name is
global_name, elseusername. The image is the avatar of the user on the Discord CDN. - An answer with no Discord id answers
oauth_failed.
Residuals
Section titled “Residuals”- A Discord address that Discord did not verify can fill a new user, unverified. The user then proves the address by mail.
If it fails
Section titled “If it fails”oauth_failed: the token call or the profile call failed. Check the client id, the secret, and the redirect.oauth_denied: the person refused at Discord.email_taken_unverified: another user holds the address, and Discord did not verify it.invalid_token: the state is spent or old, or the callback came to another browser. Start again.sign_up_closed: the sign-up rule refused a new user.