1+ import { MESSAGE_TYPES } from '@config/webview' ;
12import { webPath } from '@router/index' ;
23
34const baseURL = import . meta. env . VITE_BASE_URL ;
@@ -6,6 +7,32 @@ const Headers = { 'Content-Type': 'application/json' } as const;
67const wait = ( timeToDelay : number ) => new Promise ( ( resolve ) => setTimeout ( resolve , timeToDelay ) ) ;
78
89const 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
1037const 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
0 commit comments