@daeryun/dr_components 0.3.67 → 0.3.69
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/tsconfig-cjs.tsbuildinfo +1 -1
- package/dist/components/page/Login/LoginShareCheckContainer.jsx +145 -87
- package/dist/components/page/Login/LoginShareCheckContainer.tsx +155 -92
- package/dist/styles/globals.css +156 -156
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import React, { ReactNode, useEffect } from 'react';
|
|
4
4
|
import { usePathname, useRouter } from 'next/navigation';
|
|
5
|
+
import Fetch from './_fetch/fetch';
|
|
5
6
|
|
|
6
7
|
export interface UserInfo {
|
|
7
8
|
email: string;
|
|
@@ -11,18 +12,74 @@ export interface UserInfo {
|
|
|
11
12
|
|
|
12
13
|
interface Props {
|
|
13
14
|
children: ReactNode;
|
|
14
|
-
setUserInfo: (user: UserInfo) => void;
|
|
15
|
+
setUserInfo: (user: UserInfo) => void;
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
const LoginShareCheckContainer = ({ children, setUserInfo }: Props) => {
|
|
18
19
|
const router = useRouter();
|
|
20
|
+
const pathName = usePathname();
|
|
19
21
|
const SESSION_KEY = process.env.NEXT_PUBLIC_USER_SESSION_STORAGE_KEY || 'loginUser';
|
|
22
|
+
|
|
23
|
+
/** 기본 게스트 유저 설정 */
|
|
20
24
|
const setGuestUser = () => {
|
|
21
25
|
const guestUser: UserInfo = { email: '', name: '', isLogin: false };
|
|
22
26
|
setUserInfo(guestUser);
|
|
23
27
|
sessionStorage.setItem(SESSION_KEY, JSON.stringify(guestUser));
|
|
24
28
|
};
|
|
25
|
-
|
|
29
|
+
|
|
30
|
+
/** 쿠키 허용 여부 확인 */
|
|
31
|
+
const checkThirdPartyCookie = async () => {
|
|
32
|
+
try {
|
|
33
|
+
const createCookieResult = await fetch(`${process.env.NEXT_PUBLIC_LAWFIRM_API}/api/app/auth`, {
|
|
34
|
+
method: 'POST',
|
|
35
|
+
credentials: 'include'
|
|
36
|
+
}).then(res => res.json());
|
|
37
|
+
|
|
38
|
+
if (createCookieResult.result) {
|
|
39
|
+
const checkCookieResult = await fetch(`${process.env.NEXT_PUBLIC_LAWFIRM_API}/api/app/auth/check`, {
|
|
40
|
+
method: 'GET',
|
|
41
|
+
credentials: 'include'
|
|
42
|
+
}).then(res => res.json());
|
|
43
|
+
|
|
44
|
+
return checkCookieResult.result && !checkCookieResult.message;
|
|
45
|
+
}
|
|
46
|
+
} catch (error) {
|
|
47
|
+
console.error('Error checking third-party cookies:', error);
|
|
48
|
+
}
|
|
49
|
+
return false;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** 쿠키 허용 시 유저 정보 조회 */
|
|
53
|
+
const fetchUserData = async (): Promise<UserInfo | null> => {
|
|
54
|
+
const result = await fetch(`${process.env.NEXT_PUBLIC_SERVICE_DOMAIN}/api/lawfirm/app/user`, {
|
|
55
|
+
method: 'GET',
|
|
56
|
+
headers: { 'Content-Type': 'application/json' },
|
|
57
|
+
cache: 'no-cache',
|
|
58
|
+
credentials: 'include'
|
|
59
|
+
}).then(res => res.json());
|
|
60
|
+
|
|
61
|
+
if (result.data?.email && result.data?.name) {
|
|
62
|
+
return { email: result.data.email, name: result.data.name, isLogin: true };
|
|
63
|
+
}
|
|
64
|
+
return null;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/** 쿠키 비허용 시 유저 정보 조회 */
|
|
68
|
+
const fetchUserDataWithoutCookie = async (): Promise<UserInfo | null> => {
|
|
69
|
+
const result = await Fetch(`/api/lawfirm/app/user`, {
|
|
70
|
+
method: 'GET',
|
|
71
|
+
headers: { 'Content-Type': 'application/json' },
|
|
72
|
+
credentials: 'include'
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
if (result.result && result.data?.email && result.data?.name) {
|
|
76
|
+
return { email: result.data.email, name: result.data.name, isLogin: true };
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/** 로그인 콜백 처리 */
|
|
82
|
+
const fetchAuthCallback = async () => {
|
|
26
83
|
return await fetch(`${process.env.NEXT_PUBLIC_LAWFIRM_API}/api/app/auth/callback`, {
|
|
27
84
|
method: 'GET',
|
|
28
85
|
headers: { 'Content-Type': 'application/json' },
|
|
@@ -30,24 +87,88 @@ const LoginShareCheckContainer = ({ children, setUserInfo }: Props) => {
|
|
|
30
87
|
credentials: 'include'
|
|
31
88
|
}).then(res => res.json());
|
|
32
89
|
};
|
|
33
|
-
const pathName = usePathname();
|
|
34
90
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
if (
|
|
40
|
-
|
|
91
|
+
/** 쿠키 허용 시 로그인 확인 및 토큰 체크 */
|
|
92
|
+
const handleLoginWithCookie = async () => {
|
|
93
|
+
const result = await fetchAuthCallback();
|
|
94
|
+
|
|
95
|
+
if (result.result && result.jwt) {
|
|
96
|
+
const tokenCheckResult = await fetch(`${process.env.NEXT_PUBLIC_SERVICE_DOMAIN}/api/auth/token`, {
|
|
97
|
+
method: 'GET',
|
|
98
|
+
headers: { 'Content-Type': 'application/json' },
|
|
99
|
+
credentials: 'include'
|
|
100
|
+
}).then(res => res.json());
|
|
101
|
+
|
|
102
|
+
if (tokenCheckResult.token) {
|
|
103
|
+
const userData = await fetchUserData();
|
|
104
|
+
if (userData) {
|
|
105
|
+
setUserInfo(userData);
|
|
106
|
+
sessionStorage.setItem(SESSION_KEY, JSON.stringify(userData));
|
|
107
|
+
} else {
|
|
108
|
+
setGuestUser();
|
|
109
|
+
}
|
|
110
|
+
} else {
|
|
111
|
+
await createTokenAndFetchUser(result.jwt);
|
|
112
|
+
}
|
|
113
|
+
} else {
|
|
114
|
+
setGuestUser();
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/** 토큰 생성 후 유저 데이터 가져오기 */
|
|
119
|
+
const createTokenAndFetchUser = async (jwt: string) => {
|
|
120
|
+
const createTokenResult = await fetch(`${process.env.NEXT_PUBLIC_SERVICE_DOMAIN}/api/auth/token`, {
|
|
121
|
+
body: JSON.stringify({ jwt }),
|
|
122
|
+
method: 'POST',
|
|
123
|
+
headers: { 'Content-Type': 'application/json' },
|
|
124
|
+
credentials: 'include'
|
|
125
|
+
}).then(res => res.json());
|
|
126
|
+
|
|
127
|
+
if (createTokenResult.token) {
|
|
128
|
+
const userData = await fetchUserData();
|
|
129
|
+
if (userData) {
|
|
130
|
+
setUserInfo(userData);
|
|
131
|
+
sessionStorage.setItem(SESSION_KEY, JSON.stringify(userData));
|
|
132
|
+
} else {
|
|
133
|
+
setGuestUser();
|
|
134
|
+
}
|
|
135
|
+
} else {
|
|
136
|
+
setGuestUser();
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
/** 쿠키 비허용 시 유저 데이터 조회 */
|
|
141
|
+
const handleLoginWithoutCookie = async () => {
|
|
142
|
+
const userData = await fetchUserDataWithoutCookie();
|
|
143
|
+
if (userData) {
|
|
144
|
+
setUserInfo(userData);
|
|
145
|
+
sessionStorage.setItem(SESSION_KEY, JSON.stringify(userData));
|
|
146
|
+
} else {
|
|
147
|
+
setGuestUser();
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/** 도메인 이동 시 인증 체크 */
|
|
152
|
+
const handlePageTransitionCheck = async () => {
|
|
153
|
+
const isCookieAllowed = await checkThirdPartyCookie();
|
|
154
|
+
if (isCookieAllowed) {
|
|
155
|
+
const result = await fetchAuthCallback();
|
|
156
|
+
const storedUser = sessionStorage.getItem(SESSION_KEY);
|
|
157
|
+
const parsedUser = storedUser ? JSON.parse(storedUser) : null;
|
|
158
|
+
|
|
159
|
+
if (parsedUser?.isLogin && !result.jwt) {
|
|
41
160
|
setGuestUser();
|
|
42
161
|
router.push('/');
|
|
43
162
|
}
|
|
44
163
|
}
|
|
45
164
|
};
|
|
46
165
|
|
|
47
|
-
|
|
48
|
-
|
|
166
|
+
/** 초기 유저 정보 세팅 */
|
|
167
|
+
const initializeUser = async () => {
|
|
168
|
+
const isCookieAllowed = await checkThirdPartyCookie();
|
|
169
|
+
|
|
170
|
+
if (isCookieAllowed) {
|
|
49
171
|
const storedUser = sessionStorage.getItem(SESSION_KEY);
|
|
50
|
-
console.log('storedUser', storedUser);
|
|
51
172
|
|
|
52
173
|
if (storedUser) {
|
|
53
174
|
try {
|
|
@@ -56,100 +177,42 @@ const LoginShareCheckContainer = ({ children, setUserInfo }: Props) => {
|
|
|
56
177
|
|
|
57
178
|
if (parsedUser.isLogin) {
|
|
58
179
|
const userData = await fetchUserData();
|
|
59
|
-
|
|
60
|
-
if (userData) {
|
|
61
|
-
setUserInfo(userData);
|
|
62
|
-
} else {
|
|
63
|
-
setGuestUser();
|
|
64
|
-
}
|
|
180
|
+
if (!userData) setGuestUser();
|
|
65
181
|
return;
|
|
66
182
|
}
|
|
67
|
-
} catch
|
|
68
|
-
console.error('Failed to parse stored user data', e);
|
|
183
|
+
} catch {
|
|
69
184
|
sessionStorage.removeItem(SESSION_KEY);
|
|
70
185
|
}
|
|
71
186
|
}
|
|
72
|
-
await
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}).then(obj => obj.json());
|
|
86
|
-
console.log('get next toekn check', tokenCheckResult);
|
|
87
|
-
|
|
88
|
-
if (tokenCheckResult.token) {
|
|
89
|
-
const userData = await fetchUserData();
|
|
90
|
-
if (userData) {
|
|
91
|
-
setUserInfo(userData);
|
|
92
|
-
sessionStorage.setItem(SESSION_KEY, JSON.stringify(userData));
|
|
93
|
-
} else {
|
|
94
|
-
setGuestUser();
|
|
95
|
-
}
|
|
96
|
-
} else {
|
|
97
|
-
const createTokenResult = await fetch(
|
|
98
|
-
`${process.env.NEXT_PUBLIC_SERVICE_DOMAIN}/api/auth/token`,
|
|
99
|
-
{
|
|
100
|
-
body: JSON.stringify({ jwt: result.jwt }),
|
|
101
|
-
method: 'POST',
|
|
102
|
-
headers: { 'Content-Type': 'application/json' },
|
|
103
|
-
credentials: 'include'
|
|
104
|
-
}
|
|
105
|
-
).then(obj => obj.json());
|
|
106
|
-
|
|
107
|
-
console.log('POST next toekn check', tokenCheckResult);
|
|
108
|
-
|
|
109
|
-
if (createTokenResult.token) {
|
|
110
|
-
const userData = await fetchUserData();
|
|
111
|
-
if (userData) {
|
|
112
|
-
setUserInfo(userData);
|
|
113
|
-
sessionStorage.setItem(SESSION_KEY, JSON.stringify(userData));
|
|
114
|
-
} else {
|
|
115
|
-
setGuestUser();
|
|
116
|
-
}
|
|
117
|
-
} else {
|
|
118
|
-
setGuestUser();
|
|
119
|
-
}
|
|
187
|
+
await handleLoginWithCookie();
|
|
188
|
+
} else {
|
|
189
|
+
const storedUser = sessionStorage.getItem(SESSION_KEY);
|
|
190
|
+
|
|
191
|
+
if (storedUser) {
|
|
192
|
+
try {
|
|
193
|
+
const parsedUser: UserInfo = JSON.parse(storedUser);
|
|
194
|
+
setUserInfo(parsedUser);
|
|
195
|
+
|
|
196
|
+
if (parsedUser.isLogin) {
|
|
197
|
+
const userData = await fetchUserDataWithoutCookie();
|
|
198
|
+
if (!userData) setGuestUser();
|
|
199
|
+
return;
|
|
120
200
|
}
|
|
121
|
-
}
|
|
122
|
-
|
|
201
|
+
} catch {
|
|
202
|
+
sessionStorage.removeItem(SESSION_KEY);
|
|
123
203
|
}
|
|
124
|
-
} catch (error) {
|
|
125
|
-
console.error('Login check failed', error);
|
|
126
|
-
setGuestUser();
|
|
127
204
|
}
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
const fetchUserData = async (): Promise<UserInfo | null> => {
|
|
131
|
-
const result = await fetch(`${process.env.NEXT_PUBLIC_SERVICE_DOMAIN}/api/lawfirm/app/user`, {
|
|
132
|
-
method: 'GET',
|
|
133
|
-
headers: { 'Content-Type': 'application/json' },
|
|
134
|
-
cache: 'no-cache',
|
|
135
|
-
credentials: 'include'
|
|
136
|
-
}).then(res => res.json());
|
|
137
205
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
name: result.data.name,
|
|
142
|
-
isLogin: true
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
return null;
|
|
146
|
-
};
|
|
206
|
+
await handleLoginWithoutCookie();
|
|
207
|
+
}
|
|
208
|
+
};
|
|
147
209
|
|
|
210
|
+
useEffect(() => {
|
|
148
211
|
initializeUser();
|
|
149
212
|
}, [setUserInfo]);
|
|
150
213
|
|
|
151
214
|
useEffect(() => {
|
|
152
|
-
|
|
215
|
+
handlePageTransitionCheck();
|
|
153
216
|
}, [pathName]);
|
|
154
217
|
|
|
155
218
|
return <>{children}</>;
|