how to auto login/ slient login with refreshToken in next-auth

Viewed 529

I'm struggling with next-auth for authorization. In our project, two domains. so the main domain has a login page, after login set accessToken in redux and refresh Token in a cookie. But subDomain does not have a login page. Because main domain makes user redirect subDomain with refresh token(http only cookie). In a subDomain call API includes a refresh Token, and then the server gives a refresh token and access token. accessToken set headers. when accessToken is expired, call Api includes a refresh token again this is our authorization process. But I can not find the way by using next-auth in the subDomain. Because the page does not have a login. I want to make an auto-login with a refresh token. How to set up the [...nextauth].ts and other file settingss

Thank you!

import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import axios from "axios";

axios.defaults.baseURL = `${process.env.NEXT_PUBLIC_API_URL}`;

export default NextAuth({
  providers: [
    CredentialsProvider({
      name: "Credentials",
      credentials: {},
      async authorize(credentials, req) {
        const response = await axios.post("/api/auth/getRefreshToken");
        return response.data;
      },
    }),
  ],
  callbacks: {
    async signIn({ user, account, profile, email, credentials }) {
      return true;
    },
    async redirect({ url, baseUrl }) {
      return baseUrl;
    },
    async session({ session, token, user }) {
      session.accessToken = token.accessToken;
      return session;
    },
    async jwt({ token, account }) {
      // Persist the OAuth access_token to the token right after signin
      if (account) {
        token.accessToken = account.access_token;
      }
      return token;
    },
  },
  // pages: {
  //   signIn: "/login",
  // },
});

0 Answers
Related