UNPKG

@spotify/web-api-ts-sdk

Version:
50 lines 2.07 kB
import AccessTokenHelpers from "./AccessTokenHelpers.js"; /** * This strategy is used when you already have an access token and want to use it. * The authentication strategy will automatically renew the token when it expires. * Designed to allow a browser-based-app to post the access token to the server and use it from there. * @constructor * @param {string} clientId - Spotify application client id. * @param {string} accessToken - The access token returned from a client side Authorization Code with PKCE flow. */ export default class ProvidedAccessTokenStrategy { clientId; accessToken; refreshTokenAction; constructor(clientId, accessToken, refreshTokenAction) { this.clientId = clientId; this.accessToken = accessToken; this.refreshTokenAction = refreshTokenAction || AccessTokenHelpers.refreshCachedAccessToken; // If the raw token from the jwt response is provided here // Calculate an absolute `expiry` value. // Caveat: If this token isn't fresh, this value will be off. // It's the responsibility of the calling code to either set a valid // expires property, or ensure expires_in accounts for any lag between // issuing and passing here. if (!this.accessToken.expires) { this.accessToken.expires = AccessTokenHelpers.calculateExpiry(this.accessToken); } } setConfiguration(_) { } async getOrCreateAccessToken() { if (this.accessToken.expires && this.accessToken.expires <= Date.now()) { const refreshed = await this.refreshTokenAction(this.clientId, this.accessToken); this.accessToken = refreshed; } return this.accessToken; } async getAccessToken() { return this.accessToken; } removeAccessToken() { this.accessToken = { access_token: "", token_type: "", expires_in: 0, refresh_token: "", expires: 0 }; } } //# sourceMappingURL=ProvidedAccessTokenStrategy.js.map