-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FIX] Example NextJS - Fixing JWT Refresh token rotation #84
base: main
Are you sure you want to change the base?
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
jwt
callback 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
account
is not present, and checking for validity in the other case. But I would suggest to invert this flow. As above, ifaccount
is present, the session is brand new, so we don't need to check for validity. In all the cases whereaccount
is 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: