Skip to content

Commit bea1c37

Browse files
committed
feat: 웹뷰 로그아웃 메시지 전송 및 세션 스토리지 정리 로직 추가
1 parent 185b249 commit bea1c37

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

src/controllers/common/base.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { MESSAGE_TYPES } from '@config/webview';
12
import { webPath } from '@router/index';
23

34
const baseURL = import.meta.env.VITE_BASE_URL;
@@ -6,6 +7,32 @@ const Headers = { 'Content-Type': 'application/json' } as const;
67
const wait = (timeToDelay: number) => new Promise((resolve) => setTimeout(resolve, timeToDelay));
78

89
const enableMock = false;
10+
const AUTH_STORAGE_KEYS = ['access_token', 'refresh_token', 'user_info'] as const;
11+
const LOGIN_FORCE_HOME_ON_CLOSE_KEY = 'login_force_home_on_close';
12+
13+
const sendWebViewLogoutMessage = (accessToken: string | null) => {
14+
const reactNativeWebView = (
15+
window as Window & typeof globalThis & { ReactNativeWebView?: { postMessage: (message: string) => void } }
16+
).ReactNativeWebView;
17+
18+
if (!reactNativeWebView || !accessToken) {
19+
return;
20+
}
21+
22+
reactNativeWebView.postMessage(
23+
JSON.stringify({
24+
type: MESSAGE_TYPES.LOGOUT,
25+
token: accessToken,
26+
}),
27+
);
28+
};
29+
30+
const clearStoredAuthInfo = () => {
31+
AUTH_STORAGE_KEYS.forEach((key) => {
32+
localStorage.removeItem(key);
33+
window.dispatchEvent(new CustomEvent('localStorageChange', { detail: { key } }));
34+
});
35+
};
936

1037
const fetchData = async (path: string, init: RequestInit = {}, isFormData: boolean = false) => {
1138
const url = `${baseURL}${path}`;
@@ -55,7 +82,11 @@ const fetchAuthData = async (path: string, init: RequestInit = {}, isFormData: b
5582
});
5683

5784
if (!reissueRes.ok) {
58-
// alert('토큰 재발급 실패. 재로그인 필요');
85+
sendWebViewLogoutMessage(token);
86+
clearStoredAuthInfo();
87+
sessionStorage.setItem('login_return_path', window.location.pathname + window.location.search);
88+
sessionStorage.removeItem('login_return_state');
89+
sessionStorage.setItem(LOGIN_FORCE_HOME_ON_CLOSE_KEY, 'true');
5990

6091
window.location.href = webPath.login;
6192

src/hooks/useSocialAuth.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ export const useSocialAuth = () => {
125125
const returnStateStr = sessionStorage.getItem('login_return_state');
126126
const returnState = returnStateStr ? JSON.parse(returnStateStr) : undefined;
127127

128+
sessionStorage.removeItem('login_force_home_on_close');
128129
sessionStorage.removeItem('login_return_path');
129130
sessionStorage.removeItem('login_return_state');
130131

src/pages/Login/Login.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ const Login = () => {
4545
];
4646

4747
const handleCancelLogin = () => {
48+
const shouldGoHome = sessionStorage.getItem('login_force_home_on_close') === 'true';
49+
50+
if (shouldGoHome) {
51+
sessionStorage.removeItem('login_force_home_on_close');
52+
sessionStorage.removeItem('login_return_path');
53+
sessionStorage.removeItem('login_return_state');
54+
navigate('/', { replace: true });
55+
return;
56+
}
57+
4858
navigate(-1);
4959
};
5060

0 commit comments

Comments
 (0)