0

I am using Identity Server as a provider for authentication. I want to get user information on my Next.js pages using the useSession hooks. After a successful login, I am able to get the user details and access token using the useSession hooks. However, the problem arises when I move away from my application, open another application or tab, and then return to my application and refresh the page. At this point, the user details and access token become null. I discovered that if I move away from my application's browser, it triggers the callback function that I have configured in my nextauth.js configuration, where the user information and access token are null. Note that I have added the required claims from the backend (i.e., the Identity Server application).

If I log in to the application, it will trigger the callback function where I can get the user information and access token. After redirecting to the pages, it will return the user information. However, after moving away from the application, it will again trigger the callback function, and this time the user information becomes null.

NOTE: Refreshing the page also will not give the user information

This is the [...nextauth.js]

import NextAuth from "next-auth";
import IdentityServer4Provider from "next-auth/providers/identity-server4";

export default NextAuth({
  providers: [
    IdentityServer4Provider({
      id: "identity-server4",
      name: "IdentityServer4",
      issuer: process.env.NEXT_PUBLIC_IDENTITY_SERVER_AUTHORITY,
      clientId: process.env.NEXT_PUBLIC_IDENTITY_SERVER_CLIENT_ID,
      clientSecret: process.env.NEXT_PUBLIC_IDENTITY_SERVER_CLIENT_SECRET,
      scope: 'openid profile email',
      authorizationUrl: `${process.env.NEXT_PUBLIC_IDENTITY_SERVER_AUTHORITY}/connect/authorize?response_type=code&scope=openid%20profile%20email`,
    }),
  ],
  session: {
    strategy: "jwt",
  },
  callbacks: {
    async jwt(token, user, account, profile, isNewUser) {
      if (token.token.account) {
        const decodedToken = token.token.account.access_token ? JSON.parse(Buffer.from(token.token.account.access_token.split('.')[1], 'base64').toString()) : {};
        token.token.profile = decodedToken;
      }

      if (user) {
        token.user = user;
      }

      return token;
    },
    async session({ session, token }) {
      if (token?.token?.profile) {
        const { email, name, fullname } = token.token.profile;
        session.user.email = email;
        session.user.name = name;
        session.user.name = fullname;
      }

      if (token?.token?.account) {
        session.access_token = token.token.account.access_token;
      }

      return session;
    },
  },
  secret: process.env.NEXTAUTH_SECRET,
  debug: true,
});

And this is my Session provider "use client";

import { SessionProvider } from "next-auth/react";

export const AuthProvider = ({ children, pageProps }) => {
   return <SessionProvider refetchOnWindowFocus={false}>
      {children}
   </SessionProvider>;
};

After adding refetchOnWindowFocus parameter on session provider now it is giving me the user information but when i refresh the page then i am not able to get user information.

0