UNPKG

@adonisjs/ally

Version:

Social authentication provider for AdonisJS

164 lines (162 loc) 4.36 kB
import { Oauth2Driver } from "../../chunk-EXGR73T6.js"; import "../../chunk-NK6X76EQ.js"; import "../../chunk-TSIMPJ6I.js"; // src/drivers/spotify.ts var SpotifyDriver = class extends Oauth2Driver { /** * @param ctx - The HTTP context * @param config - Configuration for the Spotify driver */ constructor(ctx, config) { super(ctx, config); this.config = config; this.loadState(); } config; /** * Spotify token endpoint URL. */ accessTokenUrl = "https://accounts.spotify.com/api/token"; /** * Spotify authorization endpoint URL. */ authorizeUrl = "https://accounts.spotify.com/authorize"; /** * Spotify profile endpoint URL. */ userInfoUrl = "https://api.spotify.com/v1/me"; /** * The param name for the authorization code */ codeParamName = "code"; /** * The param name for the error */ errorParamName = "error"; /** * Cookie name for storing the "spotify_oauth_state" */ stateCookieName = "spotify_oauth_state"; /** * Parameter name to be used for sending and receiving the state * from Spotify */ stateParamName = "state"; /** * Parameter name for defining the scopes */ scopeParamName = "scope"; /** * Scopes separator */ scopesSeparator = " "; /** * Configures the redirect request with default scopes and Spotify-specific * parameters like show_dialog. * * @param request - The redirect request to configure */ configureRedirectRequest(request) { request.scopes(this.config.scopes || ["user-read-email"]); request.param("response_type", "code"); request.param("grant_type", "authorization_code"); if (this.config.showDialog) { request.param("show_dialog", this.config.showDialog); } } /** * 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 */ getAuthenticatedRequest(url, token) { const request = this.httpClient(url); request.header("Authorization", `Bearer ${token}`); request.header("Accept", "application/json"); request.parseAs("json"); return request; } /** * 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 */ async getUserInfo(token, callback) { const request = this.getAuthenticatedRequest(this.userInfoUrl, token); if (typeof callback === "function") { callback(request); } const body = await request.get(); return { id: body.id, nickName: body.display_name, name: body.display_name, email: body.email, avatarUrl: body.images[0]?.url || null, emailVerificationState: "unsupported", original: body }; } /** * Check if the error from the callback indicates that the user * denied authorization. * * @returns `true` when the provider reported an access-denied error. */ accessDenied() { const error = this.getError(); if (!error) { return false; } return error === "access_denied"; } /** * 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) * ``` */ async user(callback) { const token = await this.accessToken(callback); const user = await this.getUserInfo(token.token, callback); return { ...user, token }; } /** * 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) * ``` */ async userFromToken(token, callback) { const user = await this.getUserInfo(token, callback); return { ...user, token: { token, type: "bearer" } }; } }; export { SpotifyDriver };