Skip to main content

methods/discord/server

For AI agents: see llms.txt for the complete documentation index. Markdown versions are available by adding .md to a page URL or requesting Accept: text/markdown.

The discord sign-in method, exported at @convex-dev/auth/methods/discord/server.

Type Aliases​

DiscordOptions​

type DiscordOptions = object;

App-defined config for setting up the discord method.

Properties​

PropertyTypeDescription
allowedRedirectOriginsstring[]Origins redirectTo may point at, e.g. ["https://app.example.com"] for open-redirect prevention.
componentComponentApiThe Discord oauth component instance, i.e. components.authDiscord.

DiscordProfile​

type DiscordProfile = Infer<typeof vDiscordProfile>;

DiscordUserInfo​

type DiscordUserInfo = object;

The responses the component's userinfo endpoints produce, keyed the way USER_INFO_ENDPOINTS names them. This is what Discord is trusted to return; the responses are not validated against it at runtime.

Properties​

PropertyType
userDiscordUser

Variables​

vDiscordProfile​

const vDiscordProfile: VObject<
{
avatarUrl: string;
email?: string;
emailVerified: boolean;
id: string;
name: string;
username: string;
},
{
avatarUrl: VString<string, "required">;
email: VString<string | undefined, "optional">;
emailVerified: VBoolean<boolean, "required">;
id: VString<string, "required">;
name: VString<string, "required">;
username: VString<string, "required">;
},
"required",
"id" | "username" | "email" | "name" | "emailVerified" | "avatarUrl"
>;

The account profile that the discord method produces. Discord is plain OAuth (no id_token), so identity comes from its userinfo endpoint. This is the exact shape the mapping emits, so the app can validate against it precisely.

Functions​

normalizeDiscordProfile()​

function normalizeDiscordProfile(userInfoResponses): object;

Map Discord's userinfo response to DiscordProfile.

Parameters​

ParameterType
userInfoResponsesDiscordUserInfo | undefined

Returns​

object

avatarUrl​
avatarUrl: string;
email?​
optional email?: string;
emailVerified​
emailVerified: boolean;
id​
id: string;
name​
name: string;
username​
username: string;

setupDiscord()​

function setupDiscord<UsersTable>(core, options): object;

Built-in sign-in method for Discord. Wire it up with the Discord oauth component:

const auth = setupConvexAuth({ component: components.auth });
export const { signOut, refreshSession, isAuthenticated } = auth;

export const { startSignInWithDiscord, completeSignInWithDiscord } =
setupDiscord(auth, {
component: components.authDiscord,
allowedRedirectOrigins: [
"https://app.example.com",
"http://localhost:5173",
],
}).attachUserCallbacks({ createUser: internal.users.createUserDiscord });

Setup:

  1. Install the component in convex.config.ts under httpPrefix: "/oauth/discord", binding Discord's CLIENT_ID and CLIENT_SECRET.
  2. Register <site-url>/oauth/discord/callback as a redirect URI of the application in the Discord Developer Portal.

The httpPrefix alone determines the callback URL.

Type Parameters​

Type Parameter
UsersTable extends string

Parameters​

ParameterType
coreAuthCore<UsersTable>
optionsDiscordOptions

Returns​

attachUserCallbacks()​
attachUserCallbacks(callbacks): object;

Supply the app's user callbacks (see UserCallbacks for how their args must be declared) and get the functions of this method to export.

Parameters​
ParameterType
callbacksUserCallbacks<"discord", { avatarUrl: string; email?: string; emailVerified: boolean; id: string; name: string; username: string; }, UsersTable>
Returns​

object

completeSignInWithDiscord​
completeSignInWithDiscord: RegisteredMutation<
"public",
{
code: string;
state: string;
},
Promise<
| {
status: "complete";
tokens: {
accessToken: string;
refreshToken: string;
refreshTokenExpiresAt: number;
userId: string;
};
}
| {
status: "error";
userError: {
error: "INVALID_CODE";
};
}
>
> = completeSignIn;
startSignInWithDiscord​
startSignInWithDiscord: RegisteredMutation<
"public",
{
redirectTo: string;
},
Promise<{
redirect: string;
state: string;
}>
> = startSignIn;