diff --git a/lib/auth.ts b/lib/auth.ts index acf70a9..db538ae 100644 --- a/lib/auth.ts +++ b/lib/auth.ts @@ -17,10 +17,16 @@ async function getInfoSession() { const response = await fetch(`${url}/auth/info-session`, { method: "post", headers: { - Cookie: cookies() - .getAll() - .map(({ name, value }) => `${name}=${value}`) - .join("; "), + // If the front-end and back-end are on the same domain, + // there's no need to store the refresh token in cookies, + // as the back-end will send the refresh token in a cookie. + // This function is used for different domain scenarios. + /////////////////////////////////////// + // Cookie: cookies() + // .getAll() + // .map(({ name, value }) => `${name}=${value}`) + // .join("; "), + refresh: cookies().get("refresh")?.value || "", }, }); const res = await response.json(); diff --git a/lib/axios/interceptors.ts b/lib/axios/interceptors.ts index 594c60f..a5d5f93 100644 --- a/lib/axios/interceptors.ts +++ b/lib/axios/interceptors.ts @@ -10,7 +10,13 @@ export function sleep(ms: number) { // as the back-end will send the refresh token in a cookie. // This function is used for different domain scenarios. export const setRefreshToken = (value: string) => { - Cookies.set("refreshToken", value, { expires: 365 }); + Cookies.set("refresh", value, { expires: 365 }); +}; +const injectRefreshToken = (config: InternalAxiosRequestConfig) => { + const re = Cookies.get("refresh"); + const isInject = ["/auth/info-session", "/auth/refresh"]; + if (re && config.url && isInject.includes(config.url)) + config.headers.set("refresh", re); }; import { @@ -29,6 +35,7 @@ export interface ConsoleError { export const requestInterceptor = async ( config: InternalAxiosRequestConfig ): Promise => { + injectRefreshToken(config); const token = useStore.getState().auth.token; if (token) { config.headers.set("Authorization", `Bearer ${token}`);