mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-04-03 16:51:26 +00:00
55 lines
1.6 KiB
JavaScript
55 lines
1.6 KiB
JavaScript
import logger from '@/utils/logger';
|
|
import { signOut } from 'next-auth/react';
|
|
|
|
let isSigningOut = false;
|
|
|
|
export const triggerSignOut = async () => {
|
|
if (isSigningOut || typeof window === 'undefined') return;
|
|
isSigningOut = true;
|
|
logger.warn('Session expirée, déconnexion en cours...');
|
|
await signOut({ callbackUrl: '/users/login' });
|
|
};
|
|
|
|
/**
|
|
*
|
|
* @param {*} response
|
|
* @returns
|
|
*/
|
|
export const requestResponseHandler = async (response) => {
|
|
try {
|
|
if (response.status === 401) {
|
|
// On lève une erreur plutôt que de déclencher un signOut automatique.
|
|
// Plusieurs requêtes concurrent pourraient déclencher des signOut en cascade.
|
|
// Le signOut est géré proprement via RefreshTokenError dans getAuthToken.
|
|
const body = await response.json().catch(() => ({}));
|
|
const error = new Error(
|
|
body?.detail || body?.errorMessage || 'Session expirée'
|
|
);
|
|
error.status = 401;
|
|
throw error;
|
|
}
|
|
|
|
const body = await response?.json();
|
|
if (response.ok) {
|
|
return body;
|
|
}
|
|
|
|
// Throw an error with the JSON body containing the form errors
|
|
const error = new Error(body?.errorMessage || 'Une erreur est survenue');
|
|
error.details = body;
|
|
error.response = response;
|
|
throw error;
|
|
} catch (error) {
|
|
logger.error('Une erreur est survenue lors du traitement de la réponse', {
|
|
error,
|
|
response,
|
|
});
|
|
throw error;
|
|
}
|
|
};
|
|
export const errorHandler = (error) => {
|
|
logger.error('Error:', { error });
|
|
// Handle the error here, e.g., show a notification
|
|
throw error;
|
|
};
|