Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,36 @@ 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
}
}

ngOnDestroy() {
clearInterval(this.refreshAccesTokenInterval);
}

/**
* Ensures the validity of the Spotify access token, refreshing it if necessary.
* @returns {Promise<void>}
*/
async ensureTokenValidity(): Promise<void> {
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.
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down