Files
gridpilot.gg/apps/website/app/actions/logoutAction.ts
2026-01-12 01:01:49 +01:00

42 lines
1.4 KiB
TypeScript

'use server';
import { redirect } from 'next/navigation';
import { AuthApiClient } from '@/lib/api/auth/AuthApiClient';
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
/**
* Server action for logout
*
* Performs the logout mutation by calling the API and redirects to login.
* Follows the write boundary contract: all writes enter through server actions.
*/
export async function logoutAction(): Promise<void> {
try {
// Create required dependencies for API client
const logger = new ConsoleLogger();
const errorReporter = new EnhancedErrorReporter(logger, {
showUserNotifications: false,
logToConsole: true,
reportToExternal: process.env.NODE_ENV === 'production',
});
// Get API base URL from environment
const baseUrl = process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:3001';
// Create API client instance
const apiClient = new AuthApiClient(baseUrl, errorReporter, logger);
// Call the logout API endpoint
await apiClient.logout();
// Redirect to login page after successful logout
redirect('/auth/login');
} catch (error) {
// Log error for debugging
console.error('Logout action failed:', error);
// Still redirect even if logout fails - user should be able to leave
redirect('/auth/login');
}
}