Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

Goals

Non-Goals

Background

I was writing axios interceptors to make api calls from the nextjs application. I am attaching the session stored id to the request headers in the interceptor each time and checking for response, if the request is unauthorized. If i get unauthorized i had to refresh the tokens using refresh token stored in the session.
As these axios interceptors written as server actions we can't able to update the newly fetched tokens in to the session.

Looking up for a discussion on this!
Thanks

Proposal

I was writing axios interceptors to make api calls from the nextjs application. I am attaching the session stored id to the request headers in the interceptor each time and checking for response, if the request is unauthorized. If i get unauthorized i had to refresh the tokens using refresh token stored in the session.
As these axios interceptors written as server actions we can't able to update the newly fetched tokens in to the session.

Looking up for a discussion on this!
Thanks

You must be logged in to vote

Replies: 1 comment · 3 replies

Comment options

Using unstable_update in NextAuth V5 Beta

You can use the unstable_update helper from NextAuth to update the session server-side.

1. Import unstable_update

export const { handlers, signIn, signOut, auth, unstable_update } = NextAuth({
  // your config
});

2. Create a server function to update the session

Inside a "use server" file, define a helper like this:

"use server";

import { auth, unstable_update } from "@/auth"; // adjust path accordingly
import type { Session } from "next-auth";

export const updateSession = async ({
  data,
}: {
  data: Partial<Session["user"]>;
}) => {
  const session = await auth();
  if (session) {
    await unstable_update({
      ...session,
      user: {
        ...session.user,
        ...data,
      },
    });
  }
};

3. Call updateSession wherever you need to refresh/update session data

For example, after refreshing access tokens:

const newToken = await arvasitAuth.refreshAccessToken({
  token: refreshToken,
});

if (newToken.accessToken) {
  setCookie("accessToken", newToken.accessToken);
  setCookie("refreshToken", newToken.refreshToken);

  await updateSession({
    data: {
      accessToken: newToken.accessToken,
      refreshToken: newToken.refreshToken,
    },
  });
}
You must be logged in to vote
3 replies
@OahMada
Comment options

this doesn't seem to work

@PAAPII10
Comment options

It has been working fine for me for many months. How did you do? @OahMada

@OahMada
Comment options

All my attempts with this method failed. I eventually used the client update method, the one returned by useSession. Could you share your whole code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
💡
Ideas
Labels
None yet
3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.