From d6eb2dd98e79470f46e524c6189fce773b8436f7 Mon Sep 17 00:00:00 2001 From: Staijn1 Date: Thu, 2 May 2024 20:41:51 +0200 Subject: [PATCH 1/2] Refresh Spotify token to prevent unauthorized errors --- ...otify-authentication-callback.component.ts | 8 +++--- .../spotify-authentication.service.ts | 25 +++++++++++++++++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/apps/website/src/app/pages/spotify-callback-page/spotify-authentication-callback.component.ts b/apps/website/src/app/pages/spotify-callback-page/spotify-authentication-callback.component.ts index 7bdc0759..d9e767fe 100644 --- a/apps/website/src/app/pages/spotify-callback-page/spotify-authentication-callback.component.ts +++ b/apps/website/src/app/pages/spotify-callback-page/spotify-authentication-callback.component.ts @@ -19,8 +19,10 @@ export class SpotifyAuthenticationCallbackComponent implements OnInit { } ngOnInit(): void { - this.spotifyAuth.completeLogin() - .then(() => this.router.navigate(['/visualizer'])) - .catch(() => this.authenticationSuccessfull = false); + this.spotifyAuth.ensureTokenValidity().then(() => { + this.spotifyAuth.completeLogin() + .then(() => this.router.navigate(['/visualizer'])) + .catch(() => this.authenticationSuccessfull = false); + }).catch(() => this.authenticationSuccessfull = false); } } diff --git a/apps/website/src/app/services/spotify-authentication/spotify-authentication.service.ts b/apps/website/src/app/services/spotify-authentication/spotify-authentication.service.ts index cad07d77..99818d2f 100644 --- a/apps/website/src/app/services/spotify-authentication/spotify-authentication.service.ts +++ b/apps/website/src/app/services/spotify-authentication/spotify-authentication.service.ts @@ -39,8 +39,8 @@ export class SpotifyAuthenticationService implements OnDestroy { console.log("Starting refresh token interval"); clearInterval(this.refreshAccesTokenInterval); this.refreshAccesTokenInterval = setInterval(() => { - this.refreshAccessToken().catch(e => this.messageService.setMessage(e)); - }, 60000); // Refresh token every minute + this.ensureTokenValidity().catch(e => this.messageService.setMessage(e)); + }, 600000); // Refresh token every 10 minutes } } @@ -48,6 +48,27 @@ export class SpotifyAuthenticationService implements OnDestroy { clearInterval(this.refreshAccesTokenInterval); } + /** + * Ensures the validity of the Spotify access token, refreshing it if necessary. + * @returns {Promise} + */ + async ensureTokenValidity(): Promise { + const tokenSet = JSON.parse(sessionStorage.getItem("spotifyToken") as string); + if (!tokenSet) { + console.log("No Spotify token set found."); + return; + } + + const now = new Date(); + const expiresAt = new Date(tokenSet.expires_at); + if (now >= expiresAt) { + console.log("Spotify token has expired, refreshing..."); + await this.refreshAccessToken(); + } else { + console.log("Spotify token is still valid."); + } + } + /** * Returns the URL to start the authentication process. * The user will be redirected to this URL, where he will have to authorize with Spotify. From a8579d39c8161798689130b818bac20a53b419e2 Mon Sep 17 00:00:00 2001 From: Stein Jonker Date: Thu, 2 May 2024 22:08:53 +0200 Subject: [PATCH 2/2] Upgrade to audiomotion beta to test --- package-lock.json | 8 ++++---- package.json | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5f8576c0..5a065604 100644 --- a/package-lock.json +++ b/package-lock.json @@ -37,7 +37,7 @@ "@ngrx/store": "17.0.1", "@ngrx/store-devtools": "17.0.1", "aos": "^2.3.4", - "audiomotion-analyzer": "^4.4.0", + "audiomotion-analyzer": "^4.5.0-beta.1", "class-transformer": "^0.5.1", "class-validator": "^0.14.1", "daisyui": "^4.10.1", @@ -9305,9 +9305,9 @@ "dev": true }, "node_modules/audiomotion-analyzer": { - "version": "4.4.0", - "resolved": "https://registry.npmjs.org/audiomotion-analyzer/-/audiomotion-analyzer-4.4.0.tgz", - "integrity": "sha512-vba/mHQPF31NkLD+5K/0yRcmd33aaBqKx0T48dav/vc+2AQAizHCMFLxROTsVE27HsGdacwC9H1DsV3YazeKwA==", + "version": "4.5.0-beta.1", + "resolved": "https://registry.npmjs.org/audiomotion-analyzer/-/audiomotion-analyzer-4.5.0-beta.1.tgz", + "integrity": "sha512-xCVicykgFLd5cx5zd98nCdrHmWBNSSJrr9zvA/NUO494z2rOLG5vWLArBCB0BO0C8jo8at2Px+G9+h1+qxrgbw==", "funding": { "type": "Ko-fi", "url": "https://ko-fi.com/hvianna" diff --git a/package.json b/package.json index 7062aa3e..139ff176 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "@ngrx/store": "17.0.1", "@ngrx/store-devtools": "17.0.1", "aos": "^2.3.4", - "audiomotion-analyzer": "^4.4.0", + "audiomotion-analyzer": "^4.5.0-beta.1", "class-transformer": "^0.5.1", "class-validator": "^0.14.1", "daisyui": "^4.10.1",