Skip to content

Commit

Permalink
feat: (#537) 로그아웃 api 함수 구현 및 적용 msw, 테스트 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilpop8663 committed Sep 7, 2023
1 parent 02a538d commit ed2a938
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
15 changes: 15 additions & 0 deletions frontend/__test__/api/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ import {
getUserInfo,
modifyNickname,
transformUserInfoResponse,
logoutUser,
} from '@api/userInfo';

import { REFRESH_EXPIRATION_TIME } from '@constants/token';

import { getCookie, setCookie } from '@utils/cookie';

import { MOCK_USER_INFO } from '@mocks/mockData/user';

describe('서버와 통신하여 유저의 정보를 불러올 수 있어야 한다.', () => {
Expand Down Expand Up @@ -42,4 +47,14 @@ describe('서버와 통신하여 유저의 정보를 불러올 수 있어야 한

expect(MOCK_USER_INFO.nickname).toBe('cancel');
});

test('유저가 로그아웃을 한다', async () => {
setCookie({ key: 'hasEssentialInfo', maxAge: REFRESH_EXPIRATION_TIME, value: 'REFRESH!!' });

await logoutUser();

const result = getCookie().hasEssentialInfo;

expect(result).toBe(undefined);
});
});
4 changes: 4 additions & 0 deletions frontend/src/api/userInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ export const withdrawalMembership = async () => {
export const updateUserInfo = async (userInfo: UpdateUserInfoRequest) => {
await patchFetch<UpdateUserInfoRequest>(`${BASE_URL}/members/me/detail`, userInfo);
};

export const logoutUser = async () => {
await fetch('/auth/logout', { method: 'DELETE' });
};
3 changes: 3 additions & 0 deletions frontend/src/hooks/context/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { LoggedInfo } from '@type/user';

import { useUserInfo } from '@hooks/query/user/useUserInfo';

import { logoutUser } from '@api/userInfo';

import { ACCESS_TOKEN_KEY } from '@constants/localStorage';

import { clearCookie } from '@utils/cookie';
Expand All @@ -30,6 +32,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
const clearLoggedInfo = () => {
removeLocalStorage(ACCESS_TOKEN_KEY);
clearCookie('hasEssentialInfo');
logoutUser();

setLoggedInfo(notLoggedInfo);
};
Expand Down
11 changes: 11 additions & 0 deletions frontend/src/mocks/userInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,15 @@ export const mockUserInfo = [

return res(ctx.status(204));
}),

rest.delete('/auth/logout', (req, res, ctx) => {
const expirationTime = new Date(Date.now() - 1);

return res(
ctx.status(204),
ctx.cookie('hasEssentialInfo', 'expired', {
expires: expirationTime,
})
);
}),
];

0 comments on commit ed2a938

Please sign in to comment.