[FIX] Example NextJS - Fixing JWT Refresh token rotation #84
[FIX] Example NextJS - Fixing JWT Refresh token rotation #84matmont wants to merge 1 commit intospotify:mainfrom
Conversation
|
I gave this solution a try since it looks much clearer. When testing it, I was getting redirected in an infinite loop to the authentication page since the authentication was rejected and the response was HTML with the authentication page, triggering the error Solved this problem by changing the fetch call in the refreshAccessToken function in the SpotifyProfile.ts file: const response = await fetch("https://accounts.spotify.com/api/token", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Authorization: `Basic ${Buffer.from(
`${process.env.SPOTIFY_CLIENT_ID}:${process.env.SPOTIFY_CLIENT_SECRET}`
).toString("base64")}`,
},
body: `grant_type=refresh_token&refresh_token=${token.refresh_token}`,
cache: "no-cache",
});However, the main problem for the token rotation remains when calling the sdk, even with this new change. async function createPlaylist(title: string, prompt: string) {
try {
const playlist = await sdk.playlists.createPlaylist(process.env.SPOTIFY_USER_ID || '', {
name: title,
public: false,
collaborative: true,
description: prompt
});
return {
id: playlist.id,
url: playlist.external_urls.spotify
} as Playlist;
} catch (error) {
console.error('Error creating playlist', error);
}
}The error in the server logs comes as Do you know any solution for this? |
|
@jcbraz Did you ever manage to find a solution for getting the refreshed token to the sdk? |
|
I had the same problem as @jcbraz, I modified your solution a bit and haven't experienced any problems so far (@ingadi): Changed the refreshAccesToken function a bit: There was a problem with the expiration check logic that should be fixed by this. |
|
@holnburger Nice. Somewhat related but did you ever experience an issue where once the token expired and was refreshed you had to reload the page for the new token to get picked up? |
Problem
I'm working on some personal side project using Spotify Web API, and I was checking out how to handle authentication in a correct way. I'm using NextJS so I checked the Next example of the SDK as long as the NextAuth.js documentation. Checking in the code I found something that seemed a bit off to me, in the
jwtcallback of theauthOptions.Looking at the code, we can see that the first check done is on the definition of
account. As per NextAuth.js documentation:In the code we are now just returning the token as it is if
accountis not present, and checking for validity in the other case. But I would suggest to invert this flow. As above, ifaccountis present, the session is brand new, so we don't need to check for validity. In all the cases whereaccountis not defined, we then should check for validty, refreshing the token if necessary (proactively strategy).Solution
I changed the code to resemble the flow suggested above. The idea is to check if the session is brand new:
I hope that my understanding about the issue is right.
Sources/References: