mirror of
https://github.com/AutoMaker-Org/automaker.git
synced 2026-01-31 20:03:37 +00:00
fix: restore auth and auto-open last project
This commit is contained in:
@@ -13,7 +13,15 @@
|
||||
|
||||
import { useReducer, useEffect, useRef } from 'react';
|
||||
import { useNavigate } from '@tanstack/react-router';
|
||||
import { login, getHttpApiClient, getServerUrlSync } from '@/lib/http-api-client';
|
||||
import {
|
||||
login,
|
||||
getHttpApiClient,
|
||||
getServerUrlSync,
|
||||
getApiKey,
|
||||
getSessionToken,
|
||||
initApiKey,
|
||||
waitForApiKeyInit,
|
||||
} from '@/lib/http-api-client';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { KeyRound, AlertCircle, Loader2, RefreshCw, ServerCrash } from 'lucide-react';
|
||||
@@ -92,6 +100,7 @@ function reducer(state: State, action: Action): State {
|
||||
|
||||
const MAX_RETRIES = 5;
|
||||
const BACKOFF_BASE_MS = 400;
|
||||
const NO_STORE_CACHE_MODE: RequestCache = 'no-store';
|
||||
|
||||
// =============================================================================
|
||||
// Imperative Flow Logic (runs once on mount)
|
||||
@@ -102,7 +111,9 @@ const BACKOFF_BASE_MS = 400;
|
||||
* Unlike the httpClient methods, this does NOT call handleUnauthorized()
|
||||
* which would navigate us away to /logged-out.
|
||||
*
|
||||
* Relies on HTTP-only session cookie being sent via credentials: 'include'.
|
||||
* Supports both:
|
||||
* - Electron mode: Uses X-API-Key header (API key from IPC)
|
||||
* - Web mode: Uses HTTP-only session cookie
|
||||
*
|
||||
* Returns: { authenticated: true } or { authenticated: false }
|
||||
* Throws: on network errors (for retry logic)
|
||||
@@ -110,9 +121,31 @@ const BACKOFF_BASE_MS = 400;
|
||||
async function checkAuthStatusSafe(): Promise<{ authenticated: boolean }> {
|
||||
const serverUrl = getServerUrlSync();
|
||||
|
||||
// Wait for API key to be initialized before checking auth
|
||||
// This ensures we have a valid API key to send in the header
|
||||
await waitForApiKeyInit();
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
|
||||
// Electron mode: use API key header
|
||||
const apiKey = getApiKey();
|
||||
if (apiKey) {
|
||||
headers['X-API-Key'] = apiKey;
|
||||
}
|
||||
|
||||
// Add session token header if available (web mode)
|
||||
const sessionToken = getSessionToken();
|
||||
if (sessionToken) {
|
||||
headers['X-Session-Token'] = sessionToken;
|
||||
}
|
||||
|
||||
const response = await fetch(`${serverUrl}/api/auth/status`, {
|
||||
credentials: 'include', // Send HTTP-only session cookie
|
||||
headers,
|
||||
credentials: 'include',
|
||||
signal: AbortSignal.timeout(5000),
|
||||
cache: NO_STORE_CACHE_MODE,
|
||||
});
|
||||
|
||||
// Any response means server is reachable
|
||||
@@ -246,6 +279,14 @@ export function LoginView() {
|
||||
const [state, dispatch] = useReducer(reducer, initialState);
|
||||
const retryControllerRef = useRef<AbortController | null>(null);
|
||||
|
||||
// Initialize API key before checking session
|
||||
// This ensures getApiKey() returns a valid value in checkAuthStatusSafe()
|
||||
useEffect(() => {
|
||||
initApiKey().catch((error) => {
|
||||
console.warn('Failed to initialize API key:', error);
|
||||
});
|
||||
}, []);
|
||||
|
||||
// Run initial server/session check on mount.
|
||||
// IMPORTANT: Do not "run once" via a ref guard here.
|
||||
// In React StrictMode (dev), effects mount -> cleanup -> mount.
|
||||
|
||||
@@ -44,6 +44,7 @@ import { getElectronAPI } from '@/lib/electron';
|
||||
import { getApiKey, getSessionToken, getServerUrlSync } from '@/lib/http-api-client';
|
||||
|
||||
const logger = createLogger('Terminal');
|
||||
const NO_STORE_CACHE_MODE: RequestCache = 'no-store';
|
||||
|
||||
// Font size constraints
|
||||
const MIN_FONT_SIZE = 8;
|
||||
@@ -504,6 +505,7 @@ export function TerminalPanel({
|
||||
const response = await fetch(`${serverUrl}/api/auth/token`, {
|
||||
headers,
|
||||
credentials: 'include',
|
||||
cache: NO_STORE_CACHE_MODE,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
|
||||
Reference in New Issue
Block a user