Skip to main content
Version: v3

Refresh Token Rotation

While NextAuth.js doesn't automatically handle access token rotation for OAuth providers yet, this functionality can be implemented using callbacks.

Source Code

A working example can be accessed here.


Server Side

Using a JWT callback and a session callback, we can persist OAuth tokens and refresh them when they expire.

Below is a sample implementation using Google's Identity Provider. Please note that the OAuth 2.0 request in the refreshAccessToken() function will vary between different providers, but the core logic should remain similar.

import NextAuth from "next-auth"
import Providers from "next-auth/providers"

"" +
new URLSearchParams({
prompt: "consent",
access_type: "offline",
response_type: "code",

* Takes a token, and returns a new token with updated
* `accessToken` and `accessTokenExpires`. If an error occurs,
* returns the old token and an error property
async function refreshAccessToken(token) {
try {
const url =
"" +
new URLSearchParams({
client_id: process.env.GOOGLE_CLIENT_ID,
client_secret: process.env.GOOGLE_CLIENT_SECRET,
grant_type: "refresh_token",
refresh_token: token.refreshToken,

const response = await fetch(url, {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
method: "POST",

const refreshedTokens = await response.json()

if (!response.ok) {
throw refreshedTokens

return {
accessToken: refreshedTokens.access_token,
accessTokenExpires: + refreshedTokens.expires_in * 1000,
refreshToken: refreshedTokens.refresh_token ?? token.refreshToken, // Fall back to old refresh token
} catch (error) {

return {
error: "RefreshAccessTokenError",

export default NextAuth({
providers: [
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbacks: {
async jwt(token, user, account) {
// Initial sign in
if (account && user) {
return {
accessToken: account.accessToken,
accessTokenExpires: + account.expires_in * 1000,
refreshToken: account.refresh_token,

// Return previous token if the access token has not expired yet
if ( < token.accessTokenExpires) {
return token

// Access token has expired, try to update it
return refreshAccessToken(token)
async session(session, token) {
if (token) {
session.user = token.user
session.accessToken = token.accessToken
session.error = token.error

return session

Client Side

The RefreshAccessTokenError error that is caught in the refreshAccessToken() method is passed all the way to the client. This means that you can direct the user to the sign in flow if we cannot refresh their token.

We can handle this functionality as a side effect:

import { signIn, useSession } from "next-auth/client";
import { useEffect } from "react";

const HomePage() {
const [session] = useSession();

useEffect(() => {
if (session?.error === "RefreshAccessTokenError") {
signIn(); // Force sign in to hopefully resolve error
}, [session]);

return (...)