fraudTech commited on
Commit
0bfc226
·
1 Parent(s): bb4de8d

auto logoff

Browse files
Files changed (1) hide show
  1. web/src/App.tsx +25 -30
web/src/App.tsx CHANGED
@@ -95,7 +95,7 @@ console.log('App Gateway URL:', GATEWAY_URL); // Debug log
95
  function App() {
96
  const [isAuthenticated, setIsAuthenticated] = useState(false);
97
  const [user, setUser] = useState<User | null>(null);
98
- const [sessionId, setSessionId] = useState<string | null>(localStorage.getItem('session_id'));
99
  const [messages, setMessages] = useState<Message[]>([]);
100
  const [input, setInput] = useState('');
101
  const [isLoading, setIsLoading] = useState(false);
@@ -106,36 +106,33 @@ function App() {
106
  const messagesContainerRef = useRef<HTMLDivElement>(null);
107
  const abortControllerRef = useRef<AbortController | null>(null);
108
 
109
- // Verify authentication via session on mount
110
  useEffect(() => {
111
- const verifyAuth = async () => {
112
- try {
113
- const headers: HeadersInit = {};
114
- if (sessionId) {
115
- headers['Authorization'] = `Bearer ${sessionId}`;
116
- }
117
-
118
- const response = await fetch(`${GATEWAY_URL}/api/auth/verify`, {
119
- headers
120
- });
121
 
122
- if (response.ok) {
123
- const data = await response.json();
124
- setUser(data.user);
125
- setIsAuthenticated(true);
126
- } else {
127
- // Clear invalid session
128
- localStorage.removeItem('session_id');
129
- setSessionId(null);
130
- }
131
- } catch (error) {
132
- console.error('Auth verification failed:', error);
133
- localStorage.removeItem('session_id');
134
- setSessionId(null);
135
  }
136
  };
137
 
138
- verifyAuth();
 
 
 
 
 
 
 
 
139
  }, [sessionId]);
140
 
141
  useEffect(() => {
@@ -486,9 +483,8 @@ function App() {
486
  };
487
 
488
  const handleLogin = (sessionId: string, userData: User) => {
489
- // Store session ID
490
  if (sessionId) {
491
- localStorage.setItem('session_id', sessionId);
492
  setSessionId(sessionId);
493
  }
494
  setUser(userData);
@@ -545,8 +541,7 @@ function App() {
545
  console.error('Logout failed:', error);
546
  }
547
 
548
- // Clear session from localStorage
549
- localStorage.removeItem('session_id');
550
  setSessionId(null);
551
  setUser(null);
552
  setIsAuthenticated(false);
 
95
  function App() {
96
  const [isAuthenticated, setIsAuthenticated] = useState(false);
97
  const [user, setUser] = useState<User | null>(null);
98
+ const [sessionId, setSessionId] = useState<string | null>(null);
99
  const [messages, setMessages] = useState<Message[]>([]);
100
  const [input, setInput] = useState('');
101
  const [isLoading, setIsLoading] = useState(false);
 
106
  const messagesContainerRef = useRef<HTMLDivElement>(null);
107
  const abortControllerRef = useRef<AbortController | null>(null);
108
 
109
+ // Logout on page close or refresh
110
  useEffect(() => {
111
+ const handleBeforeUnload = async () => {
112
+ if (sessionId) {
113
+ // Use sendBeacon for reliable logout on page unload
114
+ const headers: HeadersInit = {
115
+ 'Authorization': `Bearer ${sessionId}`,
116
+ 'Content-Type': 'application/json'
117
+ };
 
 
 
118
 
119
+ // sendBeacon is more reliable than fetch during page unload
120
+ navigator.sendBeacon(
121
+ `${GATEWAY_URL}/api/auth/logout`,
122
+ JSON.stringify({})
123
+ );
 
 
 
 
 
 
 
 
124
  }
125
  };
126
 
127
+ window.addEventListener('beforeunload', handleBeforeUnload);
128
+
129
+ return () => {
130
+ window.removeEventListener('beforeunload', handleBeforeUnload);
131
+ // Also logout on component unmount
132
+ if (sessionId) {
133
+ handleBeforeUnload();
134
+ }
135
+ };
136
  }, [sessionId]);
137
 
138
  useEffect(() => {
 
483
  };
484
 
485
  const handleLogin = (sessionId: string, userData: User) => {
486
+ // Keep session only in memory - will be lost on refresh
487
  if (sessionId) {
 
488
  setSessionId(sessionId);
489
  }
490
  setUser(userData);
 
541
  console.error('Logout failed:', error);
542
  }
543
 
544
+ // Clear session state
 
545
  setSessionId(null);
546
  setUser(null);
547
  setIsAuthenticated(false);