Skip to content
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

How to enable refresh token in v5 - ^5.0.0-beta.16 #10739

Closed
ericqqqqq opened this issue Apr 26, 2024 · 4 comments
Closed

How to enable refresh token in v5 - ^5.0.0-beta.16 #10739

ericqqqqq opened this issue Apr 26, 2024 · 4 comments
Labels
documentation Relates to documentation triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@ericqqqqq
Copy link

What is the improvement or update you wish to see?

see an sample code to implement refresh token

Is there any context that might help us understand?

after setting up the google provider, i cannot get refresh token value in the old ways - account.refresh_token is undefined.

here is how i set it up

import NextAuth from "next-auth";
import Google from "next-auth/providers/google";

export const {
  handlers: { GET, POST },
  auth,
  signIn,
  signOut
} = NextAuth({
  providers: [Google],
  pages: {
    signIn: "/signin",
  },
  session: {
    strategy: "jwt",
  },
  callbacks: {
    async jwt({ token, user, account }) {
      if (account && user) {
        return {
          ...token,
          idToken: account.id_token,
        };
      }
      return token;
    },
    async session({ session, token }) {
      session.sessionToken = token.idToken as string;
      return session;
    },
  },
});

Does the docs page already exist? Please link to it.

No response

@ericqqqqq ericqqqqq added documentation Relates to documentation triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Apr 26, 2024
@ericqqqqq
Copy link
Author

import NextAuth from "next-auth";
import { JWT } from "next-auth/jwt";
import Google from "next-auth/providers/google";

export const {
  handlers: { GET, POST },
  auth,
  signIn,
  signOut,
} = NextAuth({
  providers: [
    Google({
      authorization: {
        params: {
          prompt: "consent",
          access_type: "offline",
        },
      },
    }),
  ],
  pages: {
    signIn: "/signin",
  },
  session: {
    strategy: "jwt",
  },
  callbacks: {
    async jwt({ token, user, account }) {
      if (account && user) {
        return {
          ...token,
          id_token: account.id_token,
          refresh_token: account.refresh_token,
          expires_at: account.expires_at * 1000,
        };
      }

      if (Date.now() > token.expires_at) return await refresh(token);

      return token;
    },
    async session({ session, token }) {
      session.sessionToken = token.id_token as string;
      return session;
    },
  },
});

async function refresh(token: JWT) {
  try {
    const url = `https://oauth2.googleapis.com/token`;
    const response = await fetch(url, {
      method: "POST",
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      body: new URLSearchParams({
        client_id: process.env.AUTH_GOOGLE_ID as string,
        client_secret: process.env.AUTH_GOOGLE_SECRET as string,
        refresh_token: token.refresh_token as string,
        grant_type: "refresh_token",
      }),
    });
    const refreshedTokens = await response.json();

    if (!response.ok) {
      throw refreshedTokens;
    }
    return {
      ...token,
      id_token: refreshedTokens.id_token,
      refresh_token: refreshedTokens.refresh_token ?? token.refresh_token,
      expires_at: Date.now() + refreshedTokens.expires_in * 1000,
    };
  } catch (error) {
    return {
      ...token,
      error: "RefreshAccessTokenError",
    };
  }
}

reference: i made a version that works for me.

@ndom91
Copy link
Member

ndom91 commented Apr 28, 2024

Thanks for sharing your working example!

There's an example for the Google login parameters in the Google Provider page and a separate doc on Refresh Token Rotation, but if there's anything you see there that we could improve, we'd appreciate a PR 🙏

@ndom91 ndom91 closed this as completed Apr 28, 2024
@ericqqqqq
Copy link
Author

ericqqqqq commented Apr 29, 2024

then my issue was - I couldn't easily find documentation for v5; it's usually v3 or v4. Can you put together the v5 documentation in one place?

If you already have it, could you share the link to the v5 documentation with me?

@ndom91
Copy link
Member

ndom91 commented Apr 30, 2024

Hey @ericqqqqq yeah so the v5 one is the "refresh token rotation" link I mentioned in the previous post. It's part of our new docs site we relaunched a few weeks ago at authjs.dev.

If you find any errors or anything like that, a PR would be great 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Relates to documentation triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet
Development

No branches or pull requests

2 participants