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

리프레시 토큰 쿠키로 변경 #390

Merged
merged 3 commits into from
Jul 2, 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
9 changes: 2 additions & 7 deletions src/layouts/App/Header/LogoutModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import { useNavigate } from 'react-router-dom';
import { useSelectedFriendsStore } from 'store/useSelectedFriendsStore';
import { useUserStore } from 'store/useUserStore';

import {
clearLocalStorageItem,
getLocalStorageItem,
} from 'services/api/v1/localStorage';
import { clearLocalStorageItem } from 'services/api/v1/localStorage';
import { logout, socialLogout } from 'services/api/v1/oauth';
import {
clearSessionStorageItem,
Expand All @@ -28,18 +25,16 @@ const LogoutModal = ({ modalState, userState }: LogoutModalProps) => {
const clearSelectedFiends = useSelectedFriendsStore(
(state) => state.clearSelectedFriends,
);
const refreshToken = getLocalStorageItem('refreshToken');
const socialAccessToken = getSessionStorageItem('socialToken');
const { providerId } = useUserStore();

const handleLogout = async () => {
await logout({ refreshToken });
await logout();
await socialLogout({ providerId, socialAccessToken });

clearUser();
clearSelectedFiends();
clearSessionStorageItem();
clearLocalStorageItem('refreshToken');
clearLocalStorageItem('socialRefreshToken');

navigate('/');
Expand Down
5 changes: 1 addition & 4 deletions src/pages/Auth/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import { useUserStore } from 'store/useUserStore';

import { setLocalStorageItem } from 'services/api/v1/localStorage';
import { getKakaoOauthToken, login } from 'services/api/v1/oauth';
import { setSessionStorageItem } from 'utils/sessionStorage';

Expand All @@ -28,15 +27,13 @@
const socialAccessToken = await getKakaoOauthToken({ code });

const res = await login({ socialAccessToken });
const { accessToken, member, refreshToken } = res.data;
const { value, expiration } = refreshToken;
const { accessToken, member } = res.data;

setLocalStorageItem('refreshToken', value, expiration);
setSessionStorageItem('accessToken', accessToken);
setUserInfo(member);
navigate(loginState);
} catch (error) {
console.warn(error);

Check warning on line 36 in src/pages/Auth/index.tsx

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
// Todo: 에러 처리
}
};
Expand Down
14 changes: 2 additions & 12 deletions src/services/api/v1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,13 @@ apiV1.interceptors.response.use(

if (status === 401) {
if (error.response.data.code === 'KF003') {
const usersRefreshToken = getLocalStorageItem('refreshToken');
apiV1.defaults.headers.common.Authorization = null;
const response = await refreshAccessToken(usersRefreshToken);
const response = await refreshAccessToken();

if (response.status === 200) {
const { accessToken, refreshToken } = response.data;
const { value, expiration } = refreshToken;
const { accessToken } = response.data;

setSessionStorageItem('accessToken', accessToken);
setLocalStorageItem('refreshToken', value, expiration);

axios.defaults.headers.common.Authorization = `Bearer ${accessToken}`;
originRequest.headers.Authorization = `Bearer ${accessToken}`;
Expand All @@ -68,13 +65,6 @@ apiV1.interceptors.response.use(
originRequest.data = [...parsedOriginData];
} else if (typeof parsedOriginData === 'object') {
originRequest.data = { ...parsedOriginData };

if (parsedOriginData.refreshToken) {
originRequest.data = {
...parsedOriginData,
refreshToken: value,
};
}
}
}

Expand Down
18 changes: 10 additions & 8 deletions src/services/api/v1/members.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import { apiV1 } from '.';

type RefreshAccessTokenResponseProps = {
accessToken: string;
refreshToken: {
value: string;
expiration: number;
};
};

type RefreshSocialAccessTokenResponseProps = {
Expand All @@ -19,10 +15,16 @@ type RefreshSocialAccessTokenResponseProps = {
};
};

export const refreshAccessToken = async (
refreshToken: string,
): Promise<AxiosResponse<RefreshAccessTokenResponseProps>> => {
const response = await apiV1.post('/oauth/reissue', { refreshToken });
export const refreshAccessToken = async (): Promise<
AxiosResponse<RefreshAccessTokenResponseProps>
> => {
const response = await apiV1.post(
'/oauth/reissue',
{},
{
withCredentials: true,
},
);
return response;
};

Expand Down
18 changes: 6 additions & 12 deletions src/services/api/v1/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@
type LoginResponseProps = {
accessToken: string;
member: NonNullable<User>;
refreshToken: {
value: string;
expiration: number;
};
};

type LogoutRequestProps = {
refreshToken: string;
};

type SocialLogoutRequestProps = {
Expand Down Expand Up @@ -67,7 +59,7 @@
);
return socialToken;
} catch (error) {
console.warn(error);

Check warning on line 62 in src/services/api/v1/oauth.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
return undefined;
}
};
Expand All @@ -89,10 +81,12 @@
};

// 로그아웃 요청
export const logout = async ({
refreshToken,
}: LogoutRequestProps): Promise<AxiosResponse> => {
const response = await apiV1.post('/oauth/logout', { refreshToken });
export const logout = async (): Promise<AxiosResponse> => {
const response = await apiV1.post(
'/oauth/logout',
{},
{ withCredentials: true },
);
return response;
};

Expand Down
Loading