Skip to content

Commit

Permalink
[test]: webview token test
Browse files Browse the repository at this point in the history
  • Loading branch information
VictoryJu committed Nov 5, 2024
1 parent e7c0dac commit 289d81a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
27 changes: 26 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { useEffect } from 'react';
import { useEffect, useRef } from 'react';
import { RootRouter } from './router/RootRouter';
import { setUserAuth, useAuthStore } from './stores/useAuthStore';
import { showiOSInfo } from './webview/utils';
import { useSocket } from './hooks/socket/useSocket';

// 전역 변수로 초기 토큰 저장
let initialToken: string | null = null;
let initialUuid: string | null = null;

if (!window.handleIosWebviewToken) {
window.handleIosWebviewToken = (token, uuid) => {
showiOSInfo(`token:${token},uuid:${uuid}`);
if (token) {
// 초기 토큰 저장
initialToken = token;
initialUuid = uuid;
setUserAuth(token, uuid);
return 'success';
}
Expand All @@ -18,19 +25,32 @@ if (!window.handleIosWebviewToken) {
function App() {
const token = useAuthStore((state) => state.accessToken);
const { socket } = useSocket();
const isInitialized = useRef(false);

// 초기화 useEffect
useEffect(() => {
if (!isInitialized.current && initialToken && initialUuid) {
console.log('Initializing with stored token');
setUserAuth(initialToken, initialUuid);
isInitialized.current = true;
}
}, []);

useEffect(() => {
const handleAuthUpdate = (event: CustomEvent) => {
console.log('Auth updated:', event.detail);
if (socket) {
console.log('Reconnecting socket');
socket.disconnect();
socket.connect();
}
};

window.addEventListener('auth-update', handleAuthUpdate as EventListener);

// WebView 준비 상태 알림
if (window.webkit?.messageHandlers.webviewInit) {
console.log('Sending webviewReady');
window.webkit.messageHandlers.webviewInit.postMessage('webviewReady');
}

Expand All @@ -42,6 +62,11 @@ function App() {
};
}, [socket, token]);

// 디버깅을 위한 토큰 상태 로깅
useEffect(() => {
console.log('Current token:', token);
}, [token]);

return <RootRouter />;
}

Expand Down
11 changes: 9 additions & 2 deletions src/stores/useAuthStore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { create } from 'zustand';
import { persist, createJSONStorage } from 'zustand/middleware';
import { persist } from 'zustand/middleware';

interface AuthState {
accessToken: string | null;
Expand All @@ -13,6 +13,7 @@ export const useAuthStore = create<AuthState>()(
accessToken: null,
uuid: null,
setAuth: (token, uuid) => {
console.log('Setting auth:', { token, uuid });
set({ accessToken: token, uuid });
window.dispatchEvent(
new CustomEvent('auth-update', {
Expand All @@ -23,11 +24,17 @@ export const useAuthStore = create<AuthState>()(
}),
{
name: 'auth-storage',
storage: createJSONStorage(() => localStorage),
onRehydrateStorage: () => {
console.log('Rehydrating auth store');
return (state) => {
console.log('Rehydrated state:', state);
};
},
}
)
);

export const setUserAuth = (token: string, uuid: string) => {
console.log('setUserAuth called:', { token, uuid });
useAuthStore.getState().setAuth(token, uuid);
};

0 comments on commit 289d81a

Please sign in to comment.