@adonisjs/ally
Version:
Social authentication provider for AdonisJS
172 lines (171 loc) • 5.27 kB
TypeScript
import type { HttpContext } from '@adonisjs/core/http';
import type { HttpClient } from '@poppinss/oauth-client';
import type { DiscordScopes, DiscordToken, ApiRequestContract, DiscordDriverConfig, RedirectRequestContract } from '../types.ts';
import { Oauth2Driver } from '../abstract_drivers/oauth2.ts';
/**
* Discord OAuth2 driver for authenticating users via Discord.
* Supports fetching user profile information including username and email.
*
* @example
* ```ts
* router.get('/discord/redirect', ({ ally }) => {
* return ally.use('discord').redirect((request) => {
* request.scopes(['identify', 'email', 'guilds'])
* })
* })
*
* router.get('/discord/callback', async ({ ally }) => {
* const discord = ally.use('discord')
*
* if (discord.accessDenied()) {
* return 'Access was denied'
* }
*
* if (discord.stateMisMatch()) {
* return 'State mismatch error'
* }
*
* if (discord.hasError()) {
* return discord.getError()
* }
*
* const user = await discord.user()
* return user
* })
* ```
*/
export declare class DiscordDriver extends Oauth2Driver<DiscordToken, DiscordScopes> {
config: DiscordDriverConfig;
/**
* Discord token endpoint URL.
*/
protected accessTokenUrl: string;
/**
* Discord authorization endpoint URL.
*/
protected authorizeUrl: string;
/**
* Discord user profile endpoint URL.
*/
protected userInfoUrl: string;
/**
* The param name for the authorization code
*/
protected codeParamName: string;
/**
* The param name for the error
*/
protected errorParamName: string;
/**
* Cookie name for storing the "discord_oauth_state"
*/
protected stateCookieName: string;
/**
* Parameter name to be used for sending and receiving the state
* from Discord
*/
protected stateParamName: string;
/**
* Parameter name for defining the scopes
*/
protected scopeParamName: string;
/**
* Scopes separator
*/
protected scopesSeparator: string;
/**
* @param ctx - The HTTP context
* @param config - Configuration for the Discord driver
*/
constructor(ctx: HttpContext, config: DiscordDriverConfig);
/**
* Configures the redirect request with default scopes and Discord-specific
* parameters like prompt, guild_id, and permissions.
*
* @param request - The redirect request to configure
*/
protected configureRedirectRequest(request: RedirectRequestContract<DiscordScopes>): void;
/**
* Configures the access token request with Discord-specific requirements.
*
* @param request - The API request to configure
*/
protected configureAccessTokenRequest(request: ApiRequestContract): void;
/**
* Creates an authenticated HTTP request with the proper authorization
* header for Discord API calls.
*
* @param url - The API endpoint URL
* @param token - The access token
*/
protected getAuthenticatedRequest(url: string, token: string): HttpClient;
/**
* Fetches the authenticated user's profile information from the Discord API.
*
* @param token - The access token
* @param callback - Optional callback to customize the API request
*
* @see https://discord.com/developers/docs/resources/user#get-current-user
*/
protected getUserInfo(token: string, callback?: (request: ApiRequestContract) => void): Promise<{
id: any;
name: string;
nickName: any;
avatarUrl: string;
email: any;
emailVerificationState: "verified" | "unverified" | "unsupported";
original: any;
}>;
/**
* Check if the error from the callback indicates that the user
* denied authorization.
*/
accessDenied(): boolean;
/**
* Get the authenticated user's profile information using
* the authorization code from the callback request.
*
* @param callback - Optional callback to customize the API request
*
* @example
* ```ts
* const user = await ally.use('discord').user()
* console.log(user.name, user.email)
* ```
*/
user(callback?: (request: ApiRequestContract) => void): Promise<{
token: DiscordToken;
id: any;
name: string;
nickName: any;
avatarUrl: string;
email: any;
emailVerificationState: "verified" | "unverified" | "unsupported";
original: any;
}>;
/**
* Get the user's profile information using an existing
* access token.
*
* @param token - The Discord access token
* @param callback - Optional callback to customize the API request
*
* @example
* ```ts
* const user = await ally.use('discord').userFromToken(accessToken)
* ```
*/
userFromToken(token: string, callback?: (request: ApiRequestContract) => void): Promise<{
token: {
token: string;
type: "bearer";
};
id: any;
name: string;
nickName: any;
avatarUrl: string;
email: any;
emailVerificationState: "verified" | "unverified" | "unsupported";
original: any;
}>;
}