@adonisjs/ally
Version:
Social authentication provider for AdonisJS
168 lines (167 loc) • 5.04 kB
TypeScript
import type { HttpContext } from '@adonisjs/core/http';
import type { HttpClient } from '@poppinss/oauth-client';
import type { SpotifyScopes, SpotifyToken, ApiRequestContract, SpotifyDriverConfig, RedirectRequestContract } from '../types.ts';
import { Oauth2Driver } from '../abstract_drivers/oauth2.ts';
/**
* Spotify OAuth2 driver for authenticating users via Spotify.
* Supports fetching user profile information including display name, email, and profile images.
*
* @example
* ```ts
* router.get('/spotify/redirect', ({ ally }) => {
* return ally.use('spotify').redirect((request) => {
* request.scopes(['user-read-email', 'user-read-private'])
* })
* })
*
* router.get('/spotify/callback', async ({ ally }) => {
* const spotify = ally.use('spotify')
*
* if (spotify.accessDenied()) {
* return 'Access was denied'
* }
*
* if (spotify.stateMisMatch()) {
* return 'State mismatch error'
* }
*
* if (spotify.hasError()) {
* return spotify.getError()
* }
*
* const user = await spotify.user()
* return user
* })
* ```
*/
export declare class SpotifyDriver extends Oauth2Driver<SpotifyToken, SpotifyScopes> {
config: SpotifyDriverConfig;
/**
* Spotify token endpoint URL.
*/
protected accessTokenUrl: string;
/**
* Spotify authorization endpoint URL.
*/
protected authorizeUrl: string;
/**
* Spotify 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 "spotify_oauth_state"
*/
protected stateCookieName: string;
/**
* Parameter name to be used for sending and receiving the state
* from Spotify
*/
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 Spotify driver
*/
constructor(ctx: HttpContext, config: SpotifyDriverConfig);
/**
* Configures the redirect request with default scopes and Spotify-specific
* parameters like show_dialog.
*
* @param request - The redirect request to configure
*/
protected configureRedirectRequest(request: RedirectRequestContract<SpotifyScopes>): void;
/**
* Creates an authenticated HTTP request with the proper authorization
* header for Spotify 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 Spotify API.
*
* @param token - The access token
* @param callback - Optional callback to customize the API request
*
* @see https://developer.spotify.com/documentation/web-api/reference/get-current-users-profile
*/
protected getUserInfo(token: string, callback?: (request: ApiRequestContract) => void): Promise<{
id: any;
nickName: any;
name: any;
email: any;
avatarUrl: any;
emailVerificationState: "unsupported";
original: any;
}>;
/**
* Check if the error from the callback indicates that the user
* denied authorization.
*
* @returns `true` when the provider reported an access-denied error.
*/
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('spotify').user()
* console.log(user.name, user.email)
* ```
*/
user(callback?: (request: ApiRequestContract) => void): Promise<{
token: SpotifyToken;
id: any;
nickName: any;
name: any;
email: any;
avatarUrl: any;
emailVerificationState: "unsupported";
original: any;
}>;
/**
* Get the user's profile information using an existing
* access token.
*
* @param token - The Spotify access token
* @param callback - Optional callback to customize the API request
*
* @example
* ```ts
* const user = await ally.use('spotify').userFromToken(accessToken)
* ```
*/
userFromToken(token: string, callback?: (request: ApiRequestContract) => void): Promise<{
token: {
token: string;
type: "bearer";
};
id: any;
nickName: any;
name: any;
email: any;
avatarUrl: any;
emailVerificationState: "unsupported";
original: any;
}>;
}