Skip to content

Commit c9ed9fd

Browse files
authored
Merge pull request #162 from Team-INSERT/feat/logout
로그아웃 기능 구현
2 parents 7495c72 + 0ccb185 commit c9ed9fd

File tree

7 files changed

+46
-25
lines changed

7 files changed

+46
-25
lines changed

src/@user/hooks/useUser.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ import { useModal } from "@/@modal/hooks";
1414
import { User } from "../types";
1515
import { userContext } from "../context";
1616
import { ROLE } from "../constants";
17+
import { useLogoutMutation } from "../services/mutation.service";
1718

1819
const useUser = () => {
1920
const [user, setUser] = useAtom(userContext);
21+
const { mutate } = useLogoutMutation();
2022
const { openModal } = useModal();
2123

2224
const {
@@ -33,6 +35,8 @@ const useUser = () => {
3335
{ enabled: !!Storage.getItem(TOKEN.ACCESS) },
3436
);
3537

38+
const logout = () => mutate();
39+
3640
const isSameUser = (id: number) => {
3741
return userInfo?.id === id;
3842
};
@@ -70,6 +74,7 @@ const useUser = () => {
7074
isAdmin: userInfo?.authority === ROLE.ADMIN,
7175
role: userInfo?.role === "STUDENT" ? "학생" : "선생님",
7276
isSameUser,
77+
logout,
7378
};
7479
};
7580

src/@user/services/api.service.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import httpClient from "@/apis/httpClient";
2+
import Storage from "@/storage";
3+
4+
export const logout = async () => {
5+
const { data } = await httpClient.auth.logout({
6+
headers: { refresh_token: Storage.getItem("TOKEN:REFRESH") },
7+
});
8+
return data;
9+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { KEY } from "@/constants";
2+
import Storage from "@/storage";
3+
import { useMutation, useQueryClient } from "@tanstack/react-query";
4+
import { logout } from "./api.service";
5+
6+
export const useLogoutMutation = () => {
7+
const queryClient = useQueryClient();
8+
return useMutation(logout, {
9+
onSettled: () => {
10+
queryClient.invalidateQueries([KEY.USER]);
11+
Storage.delItem("TOKEN:ACCESS");
12+
Storage.delItem("TOKEN:REFRESH");
13+
},
14+
});
15+
};

src/apis/httpClient/httpClient.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ export class HttpClient {
9191
});
9292
}
9393

94+
logout(requestConfig?: AxiosRequestConfig) {
95+
return this.api.delete("/logout", {
96+
...HttpClient.clientConfig,
97+
...requestConfig,
98+
});
99+
}
100+
94101
put(data: unknown, requestConfig?: AxiosRequestConfig) {
95102
return this.api.put("", data, {
96103
...HttpClient.clientConfig,

src/components/common/Header/@setting/layouts/setting.tsx

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,23 @@ import styled from "styled-components";
33
import { XIcon } from "@/assets/icons";
44
import { theme, flex, font } from "@/styles";
55
import { useModal } from "@/@modal/hooks";
6+
import { useUser } from "@/@user/hooks";
7+
import { Button } from "@/components/atoms";
68

79
const SettingModal = () => {
810
const { closeModal } = useModal();
11+
const { logout } = useUser();
912

1013
return (
1114
<Modal>
1215
<ModalContent>
1316
<SettingBox>
1417
<SettingText>설정</SettingText>
15-
<ExitButton>
16-
<XIcon onClick={closeModal} />
17-
</ExitButton>
18+
<XIcon onClick={closeModal} />
1819
</SettingBox>
19-
<LogoutBox>로그아웃</LogoutBox>
20+
<Button color={theme.primary_blue} onClick={logout}>
21+
로그아웃
22+
</Button>
2023
</ModalContent>
2124
</Modal>
2225
);
@@ -34,7 +37,6 @@ const Modal = styled.div`
3437

3538
const ModalContent = styled.div`
3639
background-color: ${theme.white};
37-
padding: 1.5%;
3840
border-radius: 3%;
3941
`;
4042

@@ -47,23 +49,4 @@ const SettingText = styled.p`
4749
${font.H4};
4850
`;
4951

50-
const ExitButton = styled.div`
51-
margin-left: 17rem;
52-
`;
53-
54-
const LogoutBox = styled.div`
55-
${flex.CENTER};
56-
margin-top: 7%;
57-
padding: 2%;
58-
border-radius: 5%;
59-
background-color: ${theme.btn_background};
60-
color: ${theme.black};
61-
${font.H4};
62-
cursor: pointer;
63-
64-
&:hover {
65-
background-color: ${theme.content};
66-
}
67-
`;
68-
6952
export default SettingModal;

src/styles/font.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const fontGenerator = (
44
lineHeight: number,
55
letterSpacing: number,
66
) => `
7-
font-family: 'Pretendard', sans-serif;
87
font-weight: ${weight};
98
font-size: ${size}rem;
109
line-height: ${lineHeight}%;

src/styles/globalStyle.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { createGlobalStyle } from "styled-components";
44
import { theme } from ".";
55

66
const GlobalStyle = createGlobalStyle`
7+
@import url("https://cdn.jsdelivr.net/gh/orioncactus/[email protected]/dist/web/variable/pretendardvariable.min.css");
8+
79
* {
10+
font-family: "Pretendard Variable", Pretendard, -apple-system, BlinkMacSystemFont, system-ui, Roboto, "Helvetica Neue", "Segoe UI", "Apple SD Gothic Neo", "Noto Sans KR", "Malgun Gothic", "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", sans-serif;
811
margin: 0;
912
padding: 0;
1013
box-sizing: border-box;

0 commit comments

Comments
 (0)