mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-04-03 16:51:26 +00:00
feat: Securisation du Backend
This commit is contained in:
@ -1,4 +1,15 @@
|
||||
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
|
||||
@ -6,6 +17,18 @@ import logger from '@/utils/logger';
|
||||
*/
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user