Skip to content

Commit 39d36d5

Browse files
authored
헤더 유저검증 로직 수정 (#108)
* fix: box이름을 headerbox로 변경 * feat: TILOG_AUTH환경변수 추가 * feat: getme 커스텀훅 작성 * fix: 이벤트리스너로 리팩터링 및 로직 분리 * fix: 로그아웃 로직 수정 * feat: callback page 작성 * fix: 헤더 디자인 수정 * fix: 리덕스 삭제 * fix: 리덕스 삭제 * fix: code smell
1 parent 23ec751 commit 39d36d5

File tree

23 files changed

+187
-426
lines changed

23 files changed

+187
-426
lines changed

constants/environment/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ export const TILOG_API = !process.env.TILOG_API
55
export const TILOG_INFO_COOKIE = !process.env.TILOG_INFO_COOKIE
66
? "TILog_Info"
77
: process.env.TILOG_INFO_COOKIE;
8+
9+
export const TILOG_AUTH = !process.env.TILOG_AUTH
10+
? "http://localhost/auth/github/login"
11+
: process.env.TILOG_AUTH;

package.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,20 @@
99
"lint": "next lint"
1010
},
1111
"dependencies": {
12-
"@reduxjs/toolkit": "^1.8.1",
1312
"@til-log.lab/tilog-api": "^0.1.9",
1413
"@tiptap/react": "^2.0.0-beta.108",
1514
"@tiptap/starter-kit": "^2.0.0-beta.183",
1615
"@types/react-responsive": "^8.0.5",
1716
"axios": "^0.26.1",
1817
"date-fns": "^2.28.0",
1918
"next": "12.1.4",
20-
"next-redux-cookie-wrapper": "^2.1.2",
21-
"next-redux-wrapper": "^7.0.5",
22-
"next-transpile-modules": "^9.0.0",
2319
"react": "18.0.0",
2420
"react-dom": "18.0.0",
2521
"react-hook-form": "^7.29.0",
2622
"react-hot-toast": "^2.2.0",
2723
"react-icons": "^4.3.1",
2824
"react-query": "^3.38.0",
29-
"react-redux": "^8.0.2",
30-
"react-responsive": "^9.0.0-beta.6",
31-
"redux": "^4.2.0"
25+
"react-responsive": "^9.0.0-beta.6"
3226
},
3327
"devDependencies": {
3428
"@tailwindcss/typography": "^0.5.2",

src/components/box/HeaderBox.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { ReactNode } from "react";
2+
3+
interface NavBoxProps {
4+
children: ReactNode;
5+
}
6+
7+
const HeaderBox = ({ children }: NavBoxProps) => (
8+
<header className="border-b border-neutral-250">
9+
<div className="flex m-auto max-w-[1280px] h-20 items-center px-5">
10+
{children}
11+
</div>
12+
</header>
13+
);
14+
export default HeaderBox;

src/components/box/NavBox.tsx

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/components/molecules/LinkTo.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,18 @@ const LinkTo = ({
1616
...anchorProps
1717
}: PropsWithChildren<PropTypes>) => {
1818
return (
19-
<Link {...{ href, as, replace, scroll, shallow, prefetch, locale }}>
19+
<Link
20+
{...{
21+
...anchorProps,
22+
href,
23+
as,
24+
replace,
25+
scroll,
26+
shallow,
27+
prefetch,
28+
locale,
29+
}}
30+
>
2031
<a {...anchorProps}>{children}</a>
2132
</Link>
2233
);

src/components/molecules/Logo.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import LinkTo from "@Components/molecules/LinkTo";
22

3-
const Logo = () => {
3+
interface LogoProps {
4+
className: string;
5+
}
6+
7+
const Logo = ({ className }: LogoProps) => {
48
return (
5-
<LinkTo
6-
href="/"
7-
className="w-20 h-10 rounded-tl-full rounded-tr-full bg-neutral-800 dark:bg-neutral-300"
8-
/>
9+
<LinkTo href="/">
10+
<div
11+
className={`${className} w-20 h-10 rounded-tl-full rounded-tr-full `}
12+
/>
13+
</LinkTo>
914
);
1015
};
1116
export default Logo;

src/components/molecules/button/LoginButton.tsx

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,16 @@
11
import React from "react";
22

3-
import toast from "react-hot-toast";
4-
import { useDispatch } from "react-redux";
5-
6-
import api from "@Library/api";
7-
import ExceptionInterface from "@Library/api/exception/interface";
8-
import { userInfoSlice } from "@Redux/userInfo";
9-
import TechIcons from "@TechIcons/TechIcons";
3+
import useLogin from "@Components/molecules/button/hooks/useLogin";
4+
import { TILOG_AUTH } from "@Constants/environment";
105

116
const LoginButton = () => {
12-
const dispatch = useDispatch();
13-
7+
useLogin();
148
const handleLogin = () => {
15-
const loginWindow = window.open(
16-
`${process.env.TILOG_API}/auth/github/login`
9+
return window.open(
10+
TILOG_AUTH,
11+
"",
12+
"toolbar=no, menubar=no, width=600, height=700"
1713
);
18-
if (!loginWindow) {
19-
return toast.error("window open error");
20-
}
21-
const loginCheck = setInterval(async () => {
22-
if (loginWindow.closed) {
23-
clearInterval(loginCheck);
24-
}
25-
try {
26-
const { data } = await api.usersService.getMe();
27-
28-
if (data) {
29-
const userInfo = {
30-
...data,
31-
isLogin: true,
32-
language: "ko",
33-
};
34-
dispatch(userInfoSlice.actions.changeUserInfo(userInfo));
35-
36-
loginWindow.close();
37-
}
38-
} catch (e) {
39-
const error = e as ExceptionInterface;
40-
if (error.statusCode !== 401) {
41-
loginWindow.close();
42-
clearInterval(loginCheck);
43-
// TODO: language error handling..
44-
}
45-
}
46-
}, 1000);
4714
};
4815
return (
4916
<button
@@ -53,8 +20,8 @@ const LoginButton = () => {
5320
>
5421
<div className="flex flex-row items-center justify-center p-3 ">
5522
Login With Github
56-
<div className="ml-3 text-2xl">
57-
<TechIcons categoryName="Github" />
23+
<div className="m-auto text-2xl font-eng-sub-font-2">
24+
{/* <TechIcons categoryName="Github" /> */}
5825
</div>
5926
</div>
6027
</button>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { useEffect } from "react";
2+
3+
import useGetMe from "@Hooks/react-query/useGetMe";
4+
5+
const useLogin = () => {
6+
const { refetch } = useGetMe();
7+
useEffect(() => {
8+
window.addEventListener(
9+
"message",
10+
async (event) => {
11+
if (event.origin !== window.location.origin) return;
12+
if (event.data === "login") {
13+
refetch();
14+
}
15+
},
16+
false
17+
);
18+
}, [refetch]);
19+
};
20+
21+
export default useLogin;

src/components/molecules/button/hooks/useLogout.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import { useDispatch } from "react-redux";
2-
1+
import useGetMe from "@Hooks/react-query/useGetMe";
32
import api from "@Library/api";
43
import httpClient from "@Library/api/httpClient";
5-
import { userInfoSlice } from "@Redux/userInfo";
64

75
const useLogout = () => {
8-
const dispatch = useDispatch();
9-
return async () => {
10-
dispatch(userInfoSlice.actions.resetUserInfo());
6+
const { refetch } = useGetMe();
7+
return () => {
8+
api.authService.deleteRefreshToken();
9+
refetch();
1110
httpClient.http.defaults.headers.common.Authorization = "";
12-
await api.authService.deleteRefreshToken();
1311
};
1412
};
1513

src/components/molecules/images/UserImage.tsx

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,26 @@
11
import Image from "next/image";
22

3-
import { FaUserCircle } from "react-icons/fa";
4-
import { useSelector } from "react-redux";
5-
6-
import { userInfoSelector } from "@Redux/userInfo";
3+
import useGetMe from "@Hooks/react-query/useGetMe";
74

85
interface UserImageProps {
9-
onClick: () => void;
6+
width?: string;
7+
height?: string;
108
}
119

12-
const UserImage = ({ onClick }: UserImageProps) => {
13-
const { avatar } = useSelector(userInfoSelector);
14-
if (!avatar) {
15-
return (
16-
<FaUserCircle
17-
onClick={onClick}
18-
className="w-[50px] mr-3 rounded-full h-[50px] dark:text-neutral-300 text-neutral-800 cursor-pointer"
19-
/>
20-
);
10+
const UserImage = ({ width = "50", height = "50" }: UserImageProps) => {
11+
const { data } = useGetMe();
12+
13+
if (!data || !data.data.avatar) {
14+
return null;
2115
}
16+
2217
return (
2318
<Image
24-
onClick={onClick}
2519
className="rounded-full cursor-pointer focus:outline-none focus:ring focus:ring-signature-color"
26-
width="50"
27-
height="50"
20+
width={width}
21+
height={height}
2822
alt="avatar"
29-
src={avatar}
23+
src={data.data.avatar}
3024
/>
3125
);
3226
};

0 commit comments

Comments
 (0)