'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 { 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'); } }