Spaces:
Sleeping
Sleeping
cookies
Browse files- web/src/App.tsx +32 -59
- web/src/Login.tsx +3 -1
web/src/App.tsx
CHANGED
|
@@ -242,14 +242,11 @@ console.log('App Gateway URL:', GATEWAY_URL); // Debug log
|
|
| 242 |
|
| 243 |
function App() {
|
| 244 |
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
| 245 |
-
const [token, setToken] = useState<string | null>(sessionStorage.getItem('auth_token'));
|
| 246 |
const [user, setUser] = useState<User | null>(null);
|
| 247 |
const [messages, setMessages] = useState<Message[]>([]);
|
| 248 |
const [input, setInput] = useState('');
|
| 249 |
const [isLoading, setIsLoading] = useState(false);
|
| 250 |
const [statusMessage, setStatusMessage] = useState('');
|
| 251 |
-
const [apiKey, setApiKey] = useState(localStorage.getItem('deepseek_api_key') || '');
|
| 252 |
-
const [showSettings, setShowSettings] = useState(false);
|
| 253 |
const messagesEndRef = useRef<HTMLDivElement>(null);
|
| 254 |
const messagesContainerRef = useRef<HTMLDivElement>(null);
|
| 255 |
|
|
@@ -259,34 +256,26 @@ function App() {
|
|
| 259 |
[]
|
| 260 |
);
|
| 261 |
|
| 262 |
-
// Verify
|
| 263 |
useEffect(() => {
|
| 264 |
-
const
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
setUser(data.user);
|
| 275 |
-
setIsAuthenticated(true);
|
| 276 |
-
} else {
|
| 277 |
-
// Token invalid, clear it
|
| 278 |
-
sessionStorage.removeItem('auth_token');
|
| 279 |
-
setToken(null);
|
| 280 |
-
}
|
| 281 |
-
} catch (error) {
|
| 282 |
-
console.error('Token verification failed:', error);
|
| 283 |
-
sessionStorage.removeItem('auth_token');
|
| 284 |
-
setToken(null);
|
| 285 |
}
|
|
|
|
|
|
|
| 286 |
}
|
| 287 |
};
|
| 288 |
-
|
| 289 |
-
|
|
|
|
| 290 |
|
| 291 |
useEffect(() => {
|
| 292 |
// Only auto-scroll if user is near bottom (within 100px)
|
|
@@ -333,13 +322,13 @@ function App() {
|
|
| 333 |
try {
|
| 334 |
const response = await fetch(`${GATEWAY_URL}/api/chat`, {
|
| 335 |
method: 'POST',
|
|
|
|
| 336 |
headers: {
|
| 337 |
-
'Content-Type': 'application/json'
|
| 338 |
-
'Authorization': `Bearer ${token}`
|
| 339 |
},
|
| 340 |
body: JSON.stringify({
|
| 341 |
-
message: currentInput
|
| 342 |
-
|
| 343 |
}),
|
| 344 |
});
|
| 345 |
|
|
@@ -550,24 +539,25 @@ function App() {
|
|
| 550 |
});
|
| 551 |
};
|
| 552 |
|
| 553 |
-
const
|
| 554 |
-
|
| 555 |
-
setShowSettings(false);
|
| 556 |
-
};
|
| 557 |
-
|
| 558 |
-
const handleLogin = (authToken: string, userData: User) => {
|
| 559 |
-
setToken(authToken);
|
| 560 |
setUser(userData);
|
| 561 |
setIsAuthenticated(true);
|
| 562 |
-
sessionStorage.setItem('auth_token', authToken);
|
| 563 |
};
|
| 564 |
|
| 565 |
-
const handleLogout = () => {
|
| 566 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 567 |
setUser(null);
|
| 568 |
setIsAuthenticated(false);
|
| 569 |
setMessages([]);
|
| 570 |
-
sessionStorage.removeItem('auth_token');
|
| 571 |
};
|
| 572 |
|
| 573 |
if (!isAuthenticated) {
|
|
@@ -588,23 +578,6 @@ function App() {
|
|
| 588 |
</button>
|
| 589 |
</div>
|
| 590 |
</header>
|
| 591 |
-
{showSettings && (
|
| 592 |
-
<div className="settings-panel">
|
| 593 |
-
<h3>Settings</h3>
|
| 594 |
-
<div className="setting-group">
|
| 595 |
-
<label>DeepSeek API Key:</label>
|
| 596 |
-
<input
|
| 597 |
-
type="password"
|
| 598 |
-
value={apiKey}
|
| 599 |
-
onChange={(e) => setApiKey(e.target.value)}
|
| 600 |
-
placeholder="Enter your API key"
|
| 601 |
-
/>
|
| 602 |
-
<button onClick={saveApiKey} className="save-btn">
|
| 603 |
-
Save
|
| 604 |
-
</button>
|
| 605 |
-
</div>
|
| 606 |
-
</div>
|
| 607 |
-
)}
|
| 608 |
<div className="chat-container">
|
| 609 |
<div className="messages" ref={messagesContainerRef}>
|
| 610 |
{messages.length === 0 && (
|
|
|
|
| 242 |
|
| 243 |
function App() {
|
| 244 |
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
|
|
|
| 245 |
const [user, setUser] = useState<User | null>(null);
|
| 246 |
const [messages, setMessages] = useState<Message[]>([]);
|
| 247 |
const [input, setInput] = useState('');
|
| 248 |
const [isLoading, setIsLoading] = useState(false);
|
| 249 |
const [statusMessage, setStatusMessage] = useState('');
|
|
|
|
|
|
|
| 250 |
const messagesEndRef = useRef<HTMLDivElement>(null);
|
| 251 |
const messagesContainerRef = useRef<HTMLDivElement>(null);
|
| 252 |
|
|
|
|
| 256 |
[]
|
| 257 |
);
|
| 258 |
|
| 259 |
+
// Verify authentication via cookie on mount
|
| 260 |
useEffect(() => {
|
| 261 |
+
const verifyAuth = async () => {
|
| 262 |
+
try {
|
| 263 |
+
const response = await fetch(`${GATEWAY_URL}/api/auth/verify`, {
|
| 264 |
+
credentials: 'include' // Send cookies automatically
|
| 265 |
+
});
|
| 266 |
+
|
| 267 |
+
if (response.ok) {
|
| 268 |
+
const data = await response.json();
|
| 269 |
+
setUser(data.user);
|
| 270 |
+
setIsAuthenticated(true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
}
|
| 272 |
+
} catch (error) {
|
| 273 |
+
console.error('Auth verification failed:', error);
|
| 274 |
}
|
| 275 |
};
|
| 276 |
+
|
| 277 |
+
verifyAuth();
|
| 278 |
+
}, []);
|
| 279 |
|
| 280 |
useEffect(() => {
|
| 281 |
// Only auto-scroll if user is near bottom (within 100px)
|
|
|
|
| 322 |
try {
|
| 323 |
const response = await fetch(`${GATEWAY_URL}/api/chat`, {
|
| 324 |
method: 'POST',
|
| 325 |
+
credentials: 'include', // Send auth cookie
|
| 326 |
headers: {
|
| 327 |
+
'Content-Type': 'application/json'
|
|
|
|
| 328 |
},
|
| 329 |
body: JSON.stringify({
|
| 330 |
+
message: currentInput
|
| 331 |
+
// API key now comes from backend environment
|
| 332 |
}),
|
| 333 |
});
|
| 334 |
|
|
|
|
| 539 |
});
|
| 540 |
};
|
| 541 |
|
| 542 |
+
const handleLogin = (_authToken: string, userData: User) => {
|
| 543 |
+
// No longer store token - backend sets httpOnly cookie
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 544 |
setUser(userData);
|
| 545 |
setIsAuthenticated(true);
|
|
|
|
| 546 |
};
|
| 547 |
|
| 548 |
+
const handleLogout = async () => {
|
| 549 |
+
try {
|
| 550 |
+
await fetch(`${GATEWAY_URL}/api/auth/logout`, {
|
| 551 |
+
method: 'POST',
|
| 552 |
+
credentials: 'include'
|
| 553 |
+
});
|
| 554 |
+
} catch (error) {
|
| 555 |
+
console.error('Logout failed:', error);
|
| 556 |
+
}
|
| 557 |
+
|
| 558 |
setUser(null);
|
| 559 |
setIsAuthenticated(false);
|
| 560 |
setMessages([]);
|
|
|
|
| 561 |
};
|
| 562 |
|
| 563 |
if (!isAuthenticated) {
|
|
|
|
| 578 |
</button>
|
| 579 |
</div>
|
| 580 |
</header>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 581 |
<div className="chat-container">
|
| 582 |
<div className="messages" ref={messagesContainerRef}>
|
| 583 |
{messages.length === 0 && (
|
web/src/Login.tsx
CHANGED
|
@@ -137,6 +137,7 @@ const Login: React.FC<LoginProps> = ({ onLogin }) => {
|
|
| 137 |
|
| 138 |
const response = await fetch(`${GATEWAY_URL}${endpoint}`, {
|
| 139 |
method: 'POST',
|
|
|
|
| 140 |
headers: { 'Content-Type': 'application/json' },
|
| 141 |
body: JSON.stringify(body)
|
| 142 |
});
|
|
@@ -147,7 +148,8 @@ const Login: React.FC<LoginProps> = ({ onLogin }) => {
|
|
| 147 |
throw new Error(data.error || 'Authentication failed');
|
| 148 |
}
|
| 149 |
|
| 150 |
-
|
|
|
|
| 151 |
} catch (err: any) {
|
| 152 |
setError(err.message || 'An error occurred');
|
| 153 |
} finally {
|
|
|
|
| 137 |
|
| 138 |
const response = await fetch(`${GATEWAY_URL}${endpoint}`, {
|
| 139 |
method: 'POST',
|
| 140 |
+
credentials: 'include', // Send and receive cookies
|
| 141 |
headers: { 'Content-Type': 'application/json' },
|
| 142 |
body: JSON.stringify(body)
|
| 143 |
});
|
|
|
|
| 148 |
throw new Error(data.error || 'Authentication failed');
|
| 149 |
}
|
| 150 |
|
| 151 |
+
// Backend now sets httpOnly cookie, just pass user data
|
| 152 |
+
onLogin('', data.user);
|
| 153 |
} catch (err: any) {
|
| 154 |
setError(err.message || 'An error occurred');
|
| 155 |
} finally {
|