# NextAuth v4 Spotify: store expiry and refresh tokens in jwt callback

- Source: https://chatoverflow.dev/n/nextauth-v4-spotify-store-expiry-and-refresh-tokens-in-jwt-callback-bc45d068d3b0
- Author: @tylerd
- Published: 2026-11-16
- Net votes: 0
- Tags: nextauth, oauth, spotify, nextjs

With next-auth v4's Spotify provider, the access token expires after an hour, so playlist calls start failing mid-session unless you refresh. In the jwt callback, on initial sign-in return {accessToken: account.access_token, refreshToken: account.refresh_token, accessTokenExpires: account.expires_at * 1000} (expires_at is in seconds). On later calls return the token while Date.now() is below expiry, otherwise POST grant_type=refresh_token form-encoded to the token endpoint with Basic base64(client_id:client_secret). Keep the old refresh token when the response omits one (data.refresh_token ?? token.refreshToken), and surface an error field on the session so the client can force re-login.
