[update] - token service
This commit is contained in:
@@ -2,7 +2,7 @@ import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
|
||||
|
||||
// Define a base query function with token refresh logic
|
||||
export const baseQuery = async (args, api, extraOptions) => {
|
||||
let { data, error, } = await fetchBaseQuery({
|
||||
let result = await fetchBaseQuery({
|
||||
baseUrl: import.meta.env.VITE_BAS_URL,
|
||||
credentials: 'include',
|
||||
prepareHeaders: (headers) => {
|
||||
@@ -11,14 +11,16 @@ export const baseQuery = async (args, api, extraOptions) => {
|
||||
},
|
||||
})(args, api, extraOptions);
|
||||
|
||||
console.log(data);
|
||||
|
||||
|
||||
if (error) {
|
||||
if (result.error) {
|
||||
if (error.status === 403) {
|
||||
try {
|
||||
const { data } = await fetchBaseQuery({
|
||||
baseUrl: import.meta.env.VITE_BAS_URL,
|
||||
credentials: 'include',
|
||||
prepareHeaders: (headers) => {
|
||||
headers.set('Content-Type', 'application/json',);
|
||||
return headers;
|
||||
},
|
||||
})(
|
||||
{
|
||||
url: "/auth/user/regenerate-token",
|
||||
@@ -43,13 +45,14 @@ export const baseQuery = async (args, api, extraOptions) => {
|
||||
localStorage.clear();
|
||||
window.location.href = '/login'; // Redirect to login page
|
||||
}
|
||||
} else if (error.status === 401) {
|
||||
localStorage.clear();
|
||||
window.location.href = '/login';
|
||||
}
|
||||
}
|
||||
else if (error.status === 401) {
|
||||
localStorage.clear();
|
||||
window.location.href = '/login';
|
||||
}
|
||||
return data;
|
||||
console.log(result);
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
// Create an RTK Query API slice
|
||||
|
||||
Reference in New Issue
Block a user