Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸ”€ Deploy #289

Merged
merged 3 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 6 additions & 35 deletions src/utils/Libs/apiClient.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,15 @@
import axios from 'axios';
import BASE_HEADER from '../Config/Config.json';
import { tokenReissue } from 'api/member';
import { getToken } from './getToken';
import { setHeader } from './setHeader';
import { getRefresh } from './getRefresh';

export const apiClient = axios.create({
baseURL: process.env.NEXT_PUBLIC_BASE_URL,
headers: BASE_HEADER,
});

apiClient.interceptors.response.use(
function (response) {
return response;
},
async function (err) {
const originalRequest = err.config;
apiClient.interceptors.request.use(setHeader);

if (
err.response &&
(err.response.status === 401 || err.response.status === 403) &&
!originalRequest._retry
) {
originalRequest._retry = true;

try {
const { RefreshToken } = await getToken(null);
const tokenResponse = await tokenReissue(
'Bearer ' + RefreshToken,
null
);

if (tokenResponse && tokenResponse.newAuthorization) {
originalRequest.headers['Authorization'] =
'Bearer ' + tokenResponse.newAuthorization;
return apiClient(originalRequest);
}
} catch (refreshError) {
return Promise.reject(refreshError);
}
}

return Promise.reject(err);
}
);
apiClient.interceptors.response.use(function (response) {
return response;
}, getRefresh);
45 changes: 45 additions & 0 deletions src/utils/Libs/getRefresh.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios';
import { getToken } from './getToken';
import { tokenReissue } from 'api/member';
import { apiClient } from './apiClient';

interface TokenResponse {
newAuthorization: string;
}

export async function getRefresh(
err: AxiosError
): Promise<AxiosResponse | Promise<never>> {
const originalRequest = err.config as AxiosRequestConfig & {
_retry?: boolean;
};

if (
err.response &&
(err.response.status === 401 || err.response.status === 403) &&
!originalRequest._retry
) {
originalRequest._retry = true;

try {
const { RefreshToken } = (await getToken(null)) as {
RefreshToken: string;
};
const tokenResponse = (await tokenReissue(
'Bearer ' + RefreshToken,
null
)) as TokenResponse;

if (tokenResponse && tokenResponse.newAuthorization) {
originalRequest.headers = originalRequest.headers || {};
originalRequest.headers['Authorization'] =
'Bearer ' + tokenResponse.newAuthorization;
return apiClient(originalRequest);
}
} catch (refreshError) {
return Promise.reject(refreshError);
}
}

return Promise.reject(err);
}
18 changes: 18 additions & 0 deletions src/utils/Libs/setHeader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { AxiosRequestConfig } from 'axios';
import { getToken } from './getToken';
import { MemberController } from './requestUrls';

export const setHeader = async (config: AxiosRequestConfig) => {
if (typeof window !== 'object') return config;
const { Authorization } = await getToken(null);

if (
config.headers &&
Authorization &&
!config.url?.includes(MemberController.auth)
) {
config.headers['Authorization'] = Authorization;
}

return config;
};
Loading