Skip to content

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.

  1. Add https://<your api host>/auth/social/discord/callback to the OAuth2 redirects of the Discord application.
  2. Copy the client id and the client secret to your secret store.
  3. Set social.discord with clientId and clientSecret.
  4. Link to /auth/social/discord/start?next=/home from the sign-in page.
The Discord preset in the config
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" },
},
});
}
The start as a test
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");
});
});
  • The scope is identify email. Core reads the profile from GET /users/@me.
  • The subject is the user id, the key that Better Auth keeps. A Better Auth account row of the provider discord links with no change.
  • An address counts as verified only with verified: true in the profile. An unverified address links no user that exists.
  • The name is global_name, else username. The image is the avatar of the user on the Discord CDN.
  • An answer with no Discord id answers oauth_failed.
  • A Discord address that Discord did not verify can fill a new user, unverified. The user then proves the address by mail.
  • 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.