OAuthClient
Discord's OAuth2 login for one application. It serves the login and callback routes, keeps each user's tokens encrypted in a store, and refreshes them before they expire.
@seedcord/plugin-oauth's OAuth plugin builds one for you. Build your own to use it without
seedcord, in any server that has a Request and takes a Response.
class OAuthClient<
TScopes extends OAuthScope = never,
TExtra extends OAuthScope = never
>const oauth = new OAuthClient({
rest: new REST().setToken(process.env.DISCORD_BOT_TOKEN),
store: new Map(),
clientId: '1564002376115949650',
clientSecret: process.env.DISCORD_CLIENT_SECRET,
baseUrl: 'https://example.com',
scopes: ['guilds'],
extraScopes: ['connections']
});
// in your server's request handler
const response = await oauth.fetch(request, (outcome) =>
outcome.kind === 'login'
? Response.redirect(new URL(outcome.login.returnTo ?? '/dashboard', request.url))
: new Response('The login failed.', { status: 400 })
);
// anywhere after that
const guilds = await oauth.as(userId).guilds();
const connections = await oauth.as(userId).connections(); // undefined until they grant itMember visibility
Filter class members by access level.
OAuthClient(
{
rest,
store,
clientId,
clientSecret,
baseUrl,
routes,
scopes,
extraScopes,
keepOpen
},
input: OAuthClientOptions<TScopes, TExtra>
)Showing members with Protected visibility and higher.
TScopes
The scopes from the options. Leave it out.
TScopes extends OAuthScope = neverTExtra
The extraScopes from the options. Leave it out.
TExtra extends OAuthScope = neverpublic readonly redirectUri: stringCalls Discord as a user who logged in. user(), guilds(), guildMember(), and connections()
return typed results. get, post, put, patch, and delete take the same arguments as your REST
client's methods and reach any other route.
Each call reads the user's tokens and refreshes them first when they're about to expire. A typed call also checks that the user granted its scope before it calls Discord.
Throws:
A SeedcordError with the code PluginOAuthLoginNeeded from any call when the user never
logged in or Discord refused to refresh their tokens. A typed call also throws it when Discord
refuses the access token, and when the user's login is older than a scope you added to scopes.
Send them through the login again.
const guilds = await oauth.as(userId).guilds({ with_counts: true });
const authorization = await oauth.as(userId).get('/oauth2/@me');public authorizeUrl(
options: AuthorizeUrlOptions<TScopes, TExtra>
): stringpublic async exchange(
code: string,
codeVerifier?: string
): Promise<OAuthLogin | undefined>codeVerifierThe PKCE verifier for an authorize link that had a codeChallenge.
Throws:
A SeedcordError when the authorize link didn't request identify.
Answers a request for the login route or the callback route. Resolves to undefined for any other
request, which your server should answer itself.
The login route sends the visitor to Discord. The callback route checks that this browser started
the login, saves the user's tokens, and then calls respond. The login route also calls respond,
with an unlisted outcome, for a link that requests a scope outside extraScopes.
public async fetch(
request: Request,
respond: (outcome: LoginOutcome) => Promisable<Response>
): Promise<Response | undefined>respondReturns what the visitor's browser gets for that outcome, usually a redirect.
Deletes a user's saved tokens. It doesn't call Discord, and the user's grant there keeps working.
When several processes share one store, a refresh that's running in another one can save the tokens
back. Call revoke when the login has to end for certain.
Revokes a user's grant on Discord and deletes their saved tokens.
Discord ends every grant that the user gave your application, so tokens that another OAuthClient
saved for them stop working too. Use forget to log someone out of one place.
public async scopesOf(
userId: string
): Promise<OAuthScope[] | undefined>