From 564295ee64dfbacc37acd7b3cd56615feaebe516 Mon Sep 17 00:00:00 2001 From: FoxxMD Date: Tue, 23 Jan 2024 15:10:33 -0500 Subject: [PATCH] feat(spotify): Prepare for future spotify lib migration by writing completing credential data Migrating to @spotify/spotify-web-api-ts-sdk requires that the credentials we provide have valid expires_in/expires data and grant type. Make this migration easier by writing that data now so users upgrading later most likely do not need to re-authenticate. --- src/backend/sources/SpotifySource.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/backend/sources/SpotifySource.ts b/src/backend/sources/SpotifySource.ts index 223694b3..99a05c1f 100644 --- a/src/backend/sources/SpotifySource.ts +++ b/src/backend/sources/SpotifySource.ts @@ -298,7 +298,10 @@ export default class SpotifySource extends MemorySource { this.spotifyApi.setRefreshToken(tokenResponse.body['refresh_token']); await writeFile(this.workingCredsPath, JSON.stringify({ token: tokenResponse.body['access_token'], - refreshToken: tokenResponse.body['refresh_token'] + refreshToken: tokenResponse.body['refresh_token'], + expires: Date.now() + (tokenResponse.body['expires_in'] * 1000), + expiresIn: tokenResponse.body['expires_in'], + grant: tokenResponse.body['token_type'] })); this.logger.info('Got token from code grant authorization!'); return true; @@ -427,12 +430,17 @@ export default class SpotifySource extends MemorySource { // spotify may return a new refresh token // if it doesn't then continue to use the last refresh token we received refresh_token = this.spotifyApi.getRefreshToken(), + expires_in, + token_type } = {} } = tokenResponse; this.spotifyApi.setAccessToken(access_token); await writeFile(this.workingCredsPath, JSON.stringify({ token: access_token, refreshToken: refresh_token, + expires: Date.now() + (expires_in * 1000), + expiresIn: expires_in, + grant: token_type })); } catch (refreshError) { const error = new UpstreamError('Refreshing access token encountered an error', {cause: refreshError}); -- 2.51.2