feat(client): add auth logic
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import axios from 'axios';
|
||||
import { Access, Auth } from '../types/auth';
|
||||
import { User } from '../types/user';
|
||||
import { AuthService } from '../services/auth';
|
||||
import { AuthService } from '../services/authService';
|
||||
import axiosRetry from 'axios-retry';
|
||||
import { useAuthStore } from '../store/authStore';
|
||||
|
||||
const baseURL = `${process.env.REACT_APP_HTTP_PROTOCOL}://${process.env.REACT_APP_API_URL}/api/v1`;
|
||||
|
||||
@@ -15,7 +16,10 @@ const base = axios.create({
|
||||
});
|
||||
|
||||
base.interceptors.request.use((config) => {
|
||||
const token = localStorage.getItem('accessToken');
|
||||
if (config.url === '/auth/refresh') {
|
||||
return config;
|
||||
}
|
||||
const token = useAuthStore.getState().accessToken;
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
@@ -43,7 +47,6 @@ base.interceptors.response.use(
|
||||
async function (error) {
|
||||
console.log('error', error);
|
||||
const originalRequest = error.response.config;
|
||||
console.log('originalRequest._retry', originalRequest);
|
||||
const urlTokens = error?.request?.responseURL.split('/');
|
||||
const url = urlTokens[urlTokens.length - 1];
|
||||
console.log('url', url);
|
||||
@@ -55,26 +58,43 @@ base.interceptors.response.use(
|
||||
url !== 'logout'
|
||||
) {
|
||||
originalRequest._retry = true;
|
||||
const res = await AuthService.refresh().catch(async () => {
|
||||
try {
|
||||
await AuthService.refresh();
|
||||
return base(originalRequest);
|
||||
} catch (error) {
|
||||
await AuthService.logout();
|
||||
});
|
||||
console.log('res', res);
|
||||
return await base(originalRequest);
|
||||
return new Promise(() => {});
|
||||
}
|
||||
}
|
||||
return await Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
interface newAccess {
|
||||
accessToken: string;
|
||||
refreshToken: string;
|
||||
}
|
||||
|
||||
const api = {
|
||||
// auth
|
||||
async login(auth: Auth): Promise<Access> {
|
||||
console.log(auth);
|
||||
const response = await base.post<Access>('/auth', auth);
|
||||
// async login(auth: Auth): Promise<Access> {
|
||||
async login(auth: Auth): Promise<newAccess> {
|
||||
// const response = (await base.post) <Access> ('/auth', auth);
|
||||
const response = await base.post<newAccess>('/auth', auth);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
async refreshToken(): Promise<Access> {
|
||||
const response = await base.post<Access>('/auth/refresh');
|
||||
const token = localStorage.getItem('refreshToken');
|
||||
const response = await base.post<Access>(
|
||||
'/auth/refresh',
|
||||
{},
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
@@ -83,6 +103,13 @@ const api = {
|
||||
const response = await base.get<User>('/profile');
|
||||
return response.data;
|
||||
},
|
||||
|
||||
async getUsers(page: number, limit: number): Promise<any> {
|
||||
const response = await base.get<User[]>(
|
||||
`/account?page=${page}&limit=${limit}`
|
||||
);
|
||||
return response.data;
|
||||
},
|
||||
};
|
||||
|
||||
export default api;
|
||||
|
Reference in New Issue
Block a user