Open carledwardfp opened 1 year ago
I am also seeing this exact issue but when using Credentials provider.
However when you call getServerSession
to get the token in a server component it does return the updated token. Odd.
Related to/duplicate of #6447 ?
Hi @carledwardfp,
Few observations....
I think you'd better append your client_id
and client_secret
to your post params object rather than using them in the headers
. This should be safe to do considering the code runs in the API route i.e. serverSide. Hence your axios.post param should look like so..
const { data } = await axios.post(
SPOTIFY_REFRESH_TOKEN_URL,
{
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
grant_type: 'refresh_token',
refresh_token: token.refreshToken,
},
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
}
)
Consider simplifying your token expiration checker within your callback or separating the evaluations. i.e. the checker should be in one of both ways.
Simplify
if (Date.now() < token.accessTokenExpires) {
return token;
}
Seperate
if (token.accessTokenExpires && (Date.now() < token.accessTokenExpires)) {
return token
}
I am also working with a Spotify Provider in NextAuth and here's my configuration ...nextauth.ts
getting the same error using the credentials provider.
I implemented the refresh token rotation in my repo six months ago and it was working as expected, but now the access token is not been updated from the refresh token endpoint, when I logged the endpoint response it looks fine, it returns a new valid access token but it is not updated in the JWT returned object.
callbacks: {
async jwt({ token, user }) {
// check if user is logged but accessToken is expired
if (token.accessToken) {
const isExpired = isTokenExpired(token.accessToken);
if (isExpired) {
try {
const response = await API.post(API_URL.REFRESH_TOKEN, {
refreshToken: token.refreshToken,
});
const newAccessToken = response.data.access_token;
return {
...token,
accessToken: newAccessToken,
};
} catch (error) {
console.log(error.response.data);
return {
...token,
error: 'invalidRefreshToken',
};
}
}
}
if (user) {
return {
...token,
accessToken: user.data.access_token,
refreshToken: user.data.refresh_token,
};
}
return {
...token,
...user,
};
},
async session({ session, token }) {
session.user = token.user;
session.accessToken = token.accessToken;
session.error = token.error;
return session;
},
}
Same problem, I cant update token after the first login. Any news on this issue?
+1
Provider type
Spotify
Environment
Reproduction URL
-
Describe the issue
Since Spotify access tokens expire in 1 hour after generation, I implemented Refresh Token Rotation following this guide
refreshToken:
authOptions:
When you are past the expiration time (1 hour for spotify). The jwt calllback should refresh the token
What happens is that after refreshing the access token, the
token
returned in the jwt callback does not change theaccessTokenExpires
value causing it to refresh the token on every request. Since it always fails this checkerif (token.accessTokenExpires && Date.now() < token.accessTokenExpires) { return token }
How to reproduce
Follow the files above.
Login and note the
accessTokenExpires
returnedMake sure that you have gone past the initial expiry (1 hour)
token.accessTokenExpires && Date.now() < token.accessTokenExpires
totoken.accessTokenExpires && Date.now() > token.accessTokenExpires
to simulate expirationIt should successfully request a new access token. Make sure to note the
accessTokenExpires
again// this is where it fails
accessTokenExpires
returnedaccessTokenExpires
is not changedsample logs: I added console logs in the example above so you can check the ff log values
jwt:signin aaccount:1676000941000
jwt:refresh newToken:1676000998817
jwt:session token:1676000941000
(bug: back to the original value)Expected behavior
logs should look like this:
jwt:signin aaccount:1676000941000
jwt:refresh newToken:1676000998817
jwt:session token:1676000998817
(expected: value is updated)