Skip to content

Commit 5a9856f

Browse files
committed
fix: 사용자 인증 정보에 accessToken 추가 및 WebView 로그아웃 메시지 전송 로직 구현
1 parent d434bf5 commit 5a9856f

6 files changed

Lines changed: 21 additions & 39 deletions

File tree

src/components/Page/Stock/Header/Header.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { MESSAGE_TYPES } from '@config/webview';
22
import useAuthInfo from '@hooks/useAuthInfo';
3-
import useLocalStorageState from '@hooks/useLocalStorageState';
43
import useToast from '@hooks/useToast';
54
import useRouter from '@router/useRouter';
65
import ConfirmModal from '@components/Modal/Confirm/ConfirmModal';
@@ -22,7 +21,7 @@ import { HeaderContainer, HeaderIconButton } from './Header.Style';
2221

2322
const StockHeader = ({ stockInfo }: { stockInfo: StockDetailInfo }) => {
2423
const { navToBack } = useRouter();
25-
const { isLogin, handleNavigateLogin } = useAuthInfo();
24+
const { isLogin, accessToken, handleNavigateLogin } = useAuthInfo();
2625
const { toast, showToast, hideToast } = useToast();
2726

2827
const { data: stockPreference } = useStockPreferenceQuery(stockInfo.stockId);
@@ -31,7 +30,6 @@ const StockHeader = ({ stockInfo }: { stockInfo: StockDetailInfo }) => {
3130
const { mutate: toggleNotification } = useToggleNotificationMutation();
3231
const isBookmark = stockPreference?.isBookmarked ?? false;
3332
const isNotification = stockPreference?.isNotificationEnabled ?? false;
34-
const [accessToken] = useLocalStorageState<string>('access_token');
3533

3634
const checkAndRequestNotificationPermission = async () => {
3735
const isWebView = !!(window as any).ReactNativeWebView;

src/config/webview.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
export const MESSAGE_TYPES = {
22
OPEN_EXTERNAL_BROWSER: 'OPEN_EXTERNAL_BROWSER',
3-
OAUTH_LOGIN_EXTERNAL: 'OAUTH_LOGIN_EXTERNAL',
43
AUTH_SUCCESS: 'AUTH_SUCCESS',
54
AUTH_ERROR: 'AUTH_ERROR',
6-
TOKEN: 'TOKEN',
7-
NEED_REGISTER: 'NEED_REGISTER',
85
REQUEST_NOTIFICATION_PERMISSION: 'REQUEST_NOTIFICATION_PERMISSION',
6+
LOGOUT: 'LOGOUT',
97
} as const;
108

119
export type MessageType = (typeof MESSAGE_TYPES)[keyof typeof MESSAGE_TYPES];

src/controllers/common/base.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { webPath } from '@router/index';
2-
import { MESSAGE_TYPES } from '../../config/webview';
32

43
const baseURL = import.meta.env.VITE_BASE_URL;
54
const Headers = { 'Content-Type': 'application/json' } as const;
@@ -73,11 +72,6 @@ const fetchAuthData = async (path: string, init: RequestInit = {}, isFormData: b
7372
window.dispatchEvent(new CustomEvent('localStorageChange', { detail: { key: 'access_token' } }));
7473
window.dispatchEvent(new CustomEvent('localStorageChange', { detail: { key: 'refresh_token' } }));
7574

76-
// WebView에 새 토큰 전달
77-
(window as any).ReactNativeWebView?.postMessage(
78-
JSON.stringify({ type: MESSAGE_TYPES.TOKEN, token: access_token }),
79-
);
80-
8175
// 원래 요청 재시도
8276
res = await fetch(url, {
8377
method: 'GET',

src/hooks/useAuthInfo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const useAuthInfo = () => {
5454
removeUserInfo();
5555
};
5656

57-
return { isLogin, userInfo, beforeLoginDepth, handleNavigateLogin, setUserInfo, setAuthInfo, clearAuthInfo };
57+
return { isLogin, accessToken, userInfo, beforeLoginDepth, handleNavigateLogin, setUserInfo, setAuthInfo, clearAuthInfo };
5858
};
5959

6060
export default useAuthInfo;

src/hooks/useSocialAuth.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,6 @@ export const useSocialAuth = () => {
9393
const res = await fetchOAuth2Login(code, apiState, provider as ProviderKey);
9494

9595
if (res.state === 'NEED_REGISTER') {
96-
// WebView에서는 네이티브에 메시지 전송
97-
// if (isWebView && (window as any).ReactNativeWebView) {
98-
// (window as any).ReactNativeWebView.postMessage(
99-
// JSON.stringify({
100-
// type: MESSAGE_TYPES.NEED_REGISTER,
101-
// email: res.email,
102-
// provider,
103-
// }),
104-
// );
105-
// return;
106-
// }
107-
108-
// 브라우저에서는 회원가입 페이지로 이동
10996
navigate(webPath.register, {
11097
state: {
11198
provider,
@@ -124,16 +111,6 @@ export const useSocialAuth = () => {
124111
});
125112
setRecentProvider(provider);
126113

127-
// WebView인 경우 네이티브 앱에도 알림
128-
if (isWebView && (window as any).ReactNativeWebView) {
129-
(window as any).ReactNativeWebView.postMessage(
130-
JSON.stringify({
131-
type: MESSAGE_TYPES.TOKEN,
132-
token: res.access_token,
133-
}),
134-
);
135-
}
136-
137114
if (isWebView && (window as any).ReactNativeWebView) {
138115
(window as any).ReactNativeWebView.postMessage(
139116
JSON.stringify({

src/pages/MyPage/MyPage.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { MESSAGE_TYPES } from '@config/webview';
12
import useAuthInfo from '@hooks/useAuthInfo';
23
import useRouter from '@router/useRouter';
34
import ConfirmModal from '@components/Modal/Confirm/ConfirmModal';
@@ -22,7 +23,7 @@ import {
2223
import MyPageProfile from './Profile/Profile';
2324

2425
const MyPage = () => {
25-
const { isLogin, clearAuthInfo } = useAuthInfo();
26+
const { isLogin, accessToken, clearAuthInfo } = useAuthInfo();
2627
const { data: favorites = [] } = useBookmarkListQuery();
2728
const { data: experimentStatus } = useExperimentStatusQuery();
2829

@@ -43,9 +44,23 @@ const MyPage = () => {
4344
} = useRouter();
4445

4546
const handleLogout = async () => {
46-
await fetchAuthLogout();
47-
clearAuthInfo();
47+
const isWebView = !!(window as any).ReactNativeWebView;
48+
49+
if (isWebView) {
50+
// WebView: 앱에 로그아웃 메시지 전송 (앱이 모든 로그아웃 처리)
51+
(window as any).ReactNativeWebView?.postMessage(
52+
JSON.stringify({
53+
type: MESSAGE_TYPES.LOGOUT,
54+
token: accessToken,
55+
}),
56+
);
57+
// 앱에서 처리 후 웹뷰를 새로고침하거나 홈으로 이동시킬 것
58+
} else {
59+
// 브라우저: 웹에서 직접 로그아웃 처리
60+
await fetchAuthLogout();
61+
}
4862

63+
clearAuthInfo();
4964
window.location.href = '/';
5065
};
5166

0 commit comments

Comments
 (0)