Skip to content

Commit

Permalink
feat: MeeTeamIdle#111 토큰 재발급 API 엔드포인트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
kimsuyeon0916 committed Jul 8, 2024
1 parent 045b952 commit c7ddfa3
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { withdrawAccount } from '../../../../../service';
import secureLocalStorage from 'react-secure-storage';

const ACCESS_TOKEN_KEY = import.meta.env.VITE_ACCESS_TOKEN_KEY;
const REFRESH_TOKEN_KEY = import.meta.env.VITE_REFRESH_TOKEN_KEY;
const PLATFORM_ID = import.meta.env.VITE_PLATFORM_ID;

const AccountDelete = () => {
Expand All @@ -22,6 +23,7 @@ const AccountDelete = () => {
setUserState(null);
queryClient.clear();
secureLocalStorage.removeItem(ACCESS_TOKEN_KEY);
secureLocalStorage.removeItem(REFRESH_TOKEN_KEY);
secureLocalStorage.removeItem(PLATFORM_ID);
},
});
Expand Down
1 change: 1 addition & 0 deletions src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ interface ImportMeta {
env: {
VITE_BASE_URL: string;
VITE_ACCESS_TOKEN_KEY: string;
VITE_REFRESH_TOKEN_KEY: string;
VITE_NAVER_CLIENT_ID: string;
VITE_NAVER_STATE: string;
VITE_NAVER_CALLBACK_URL: string;
Expand Down
8 changes: 6 additions & 2 deletions src/hooks/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { User } from '../types';
import secureLocalStorage from 'react-secure-storage';

const ACCESS_TOKEN_KEY = import.meta.env.VITE_ACCESS_TOKEN_KEY;
const REFRESH_TOKEN_KEY = import.meta.env.VITE_REFRESH_TOKEN_KEY;

const PLATFORM_ID = import.meta.env.VITE_PLATFORM_ID;

Expand All @@ -36,8 +37,9 @@ export const useCheckExist = ({ onSuccess, setUserState, setLoginState }: AuthPr
return useMutation({
mutationFn: checkExist,
onSuccess: data => {
if (data?.accessToken) {
if (data?.accessToken && data?.refreshToken) {
secureLocalStorage.setItem(ACCESS_TOKEN_KEY, data.accessToken);
secureLocalStorage.setItem(REFRESH_TOKEN_KEY, data.refreshToken);
setUserState?.({
userId: data.userId,
nickname: data.nickname,
Expand All @@ -61,9 +63,10 @@ export const useNaver# = ({ onSuccess, setUserState, setLoginState }: AuthP
return useMutation({
mutationFn: #,
onSuccess: data => {
if (data?.accessToken) {
if (data?.accessToken && data?.refreshToken) {
secureLocalStorage.removeItem(PLATFORM_ID);
secureLocalStorage.setItem(ACCESS_TOKEN_KEY, data.accessToken);
secureLocalStorage.setItem(REFRESH_TOKEN_KEY, data.refreshToken);
setUserState?.({
userId: data.userId,
nickname: data.nickname,
Expand Down Expand Up @@ -131,6 +134,7 @@ export const useSignOut = ({ onSuccess, setUserState, setLoginState }: AuthProps
mutationFn: signOut,
onSuccess: () => {
secureLocalStorage.removeItem(ACCESS_TOKEN_KEY);
secureLocalStorage.removeItem(REFRESH_TOKEN_KEY);
setUserState?.(null);
setLoginState?.(false);
onSuccess?.();
Expand Down
12 changes: 12 additions & 0 deletions src/service/auth/auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Department, #Payload, University, UserReponse } from '../../types';
import { EndPoint, axiosAuthInstance, axiosInstance } from '..';
import axios from 'axios';

const platformType = 'NAVER';

Expand Down Expand Up @@ -116,3 +117,14 @@ export const withdrawAccount = async () => {
console.error(error);
}
};

export const issueToken = async () => {
try {
const response = await axios.post<UserReponse>(EndPoint.ISSUE);

return response;
} catch (error) {
console.error(error);
return null;
}
};
20 changes: 20 additions & 0 deletions src/service/axiosInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import axios, { AxiosError, AxiosResponse, InternalAxiosRequestConfig } from 'ax
import { CustomInstance } from '../types';
import qs from 'qs';
import secureLocalStorage from 'react-secure-storage';
import { issueToken } from './';

const axiosConfig = {
baseURL: import.meta.env.VITE_BASE_URL,
Expand All @@ -11,6 +12,7 @@ const axiosConfig = {
};

const ACCESS_TOKEN_KEY = import.meta.env.VITE_ACCESS_TOKEN_KEY;
const REFRESH_TOKEN_KEY = import.meta.env.VITE_REFRESH_TOKEN_KEY;

export const axiosInstance: CustomInstance = axios.create(axiosConfig);
export const axiosAuthInstance: CustomInstance = axios.create(axiosConfig);
Expand All @@ -23,6 +25,16 @@ axiosAuthInstance.defaults.paramsSerializer = params => {
return qs.stringify(params, { arrayFormat: 'repeat' });
};

const reissueToken = () => {
issueToken().then(async res => {
if (res?.status === 200 && res.data.accessToken && res.data.refreshToken) {
secureLocalStorage.setItem(ACCESS_TOKEN_KEY, res.data.accessToken);
secureLocalStorage.setItem(REFRESH_TOKEN_KEY, res.data.refreshToken);
onRequest(res.config);
}
});
};

const onResponse = (response: AxiosResponse): AxiosResponse => {
return response.data;
};
Expand All @@ -34,6 +46,14 @@ const onError = (error: AxiosError) => {
console.error(response.data);
}

if (error.config && response?.status === 403) {
const refreshToken = secureLocalStorage.getItem(REFRESH_TOKEN_KEY);

error.config.headers.Authorization = `Bearer ${refreshToken}`;

return reissueToken();
}

return Promise.reject(error);
};

Expand Down
1 change: 1 addition & 0 deletions src/service/endPoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const EndPoint = {
},
SIGN_OUT: '/auth/logout',
WITHDRAW: '/user',
ISSUE: '/auth/reissue',

/* profile */
PROFILE: {
Expand Down
2 changes: 2 additions & 0 deletions src/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
readDepartmentList,
signOut,
withdrawAccount,
issueToken,
} from './auth/auth';
import {
getPostingData,
Expand Down Expand Up @@ -80,4 +81,5 @@ export {
deletePostingRecruit,
readProfileImage,
withdrawAccount,
issueToken,
};

0 comments on commit c7ddfa3

Please # to comment.