mirror of
https://github.com/AutoMaker-Org/automaker.git
synced 2026-01-31 06:42:03 +00:00
refactor: update session cookie options and improve login view authentication flow
- Revised SameSite attribute for session cookies to clarify its behavior in documentation. - Streamlined cookie clearing logic in the authentication route by utilizing `getSessionCookieOptions()`. - Enhanced the login view to support aborting server checks, improving responsiveness during component unmounting. - Ensured proper handling of server check retries with abort signal integration for better user experience.
This commit is contained in:
@@ -3,7 +3,6 @@ import { RouterProvider } from '@tanstack/react-router';
|
||||
import { createLogger } from '@automaker/utils/logger';
|
||||
import { router } from './utils/router';
|
||||
import { SplashScreen } from './components/splash-screen';
|
||||
import { LoadingState } from './components/ui/loading-state';
|
||||
import { useSettingsSync } from './hooks/use-settings-sync';
|
||||
import { useCursorStatusInit } from './hooks/use-cursor-status-init';
|
||||
import './styles/global.css';
|
||||
|
||||
@@ -125,14 +125,25 @@ async function checkAuthStatusSafe(): Promise<{ authenticated: boolean }> {
|
||||
*/
|
||||
async function checkServerAndSession(
|
||||
dispatch: React.Dispatch<Action>,
|
||||
setAuthState: (state: { isAuthenticated: boolean; authChecked: boolean }) => void
|
||||
setAuthState: (state: { isAuthenticated: boolean; authChecked: boolean }) => void,
|
||||
signal?: AbortSignal
|
||||
): Promise<void> {
|
||||
for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
|
||||
// Return early if the component has unmounted
|
||||
if (signal?.aborted) {
|
||||
return;
|
||||
}
|
||||
|
||||
dispatch({ type: 'SERVER_CHECK_RETRY', attempt });
|
||||
|
||||
try {
|
||||
const result = await checkAuthStatusSafe();
|
||||
|
||||
// Return early if the component has unmounted
|
||||
if (signal?.aborted) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (result.authenticated) {
|
||||
// Server is reachable and we're authenticated
|
||||
setAuthState({ isAuthenticated: true, authChecked: true });
|
||||
@@ -148,10 +159,13 @@ async function checkServerAndSession(
|
||||
console.debug(`Server check attempt ${attempt}/${MAX_RETRIES} failed:`, error);
|
||||
|
||||
if (attempt === MAX_RETRIES) {
|
||||
dispatch({
|
||||
type: 'SERVER_ERROR',
|
||||
message: 'Unable to connect to server. Please check that the server is running.',
|
||||
});
|
||||
// Return early if the component has unmounted
|
||||
if (!signal?.aborted) {
|
||||
dispatch({
|
||||
type: 'SERVER_ERROR',
|
||||
message: 'Unable to connect to server. Please check that the server is running.',
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -225,7 +239,12 @@ export function LoginView() {
|
||||
if (initialCheckDone.current) return;
|
||||
initialCheckDone.current = true;
|
||||
|
||||
checkServerAndSession(dispatch, setAuthState);
|
||||
const controller = new AbortController();
|
||||
checkServerAndSession(dispatch, setAuthState, controller.signal);
|
||||
|
||||
return () => {
|
||||
controller.abort();
|
||||
};
|
||||
}, [setAuthState]);
|
||||
|
||||
// When we enter checking_setup phase, check setup status
|
||||
@@ -255,7 +274,8 @@ export function LoginView() {
|
||||
const handleRetry = () => {
|
||||
initialCheckDone.current = false;
|
||||
dispatch({ type: 'RETRY_SERVER_CHECK' });
|
||||
checkServerAndSession(dispatch, setAuthState);
|
||||
const controller = new AbortController();
|
||||
checkServerAndSession(dispatch, setAuthState, controller.signal);
|
||||
};
|
||||
|
||||
// =============================================================================
|
||||
|
||||
Reference in New Issue
Block a user