chore: ajustement JWT

This commit is contained in:
Luc SORIGNET
2025-02-22 10:52:50 +01:00
parent eb89a324ab
commit c861239d48
12 changed files with 244 additions and 75 deletions

View File

@ -1,31 +1,48 @@
import { useState } from "react"
'use client';
import { useState, useEffect } from 'react';
const useLocalStorage = (key, initialValue) => {
const [state, setState] = useState(() => {
// Initialize the state
const [storedValue, setStoredValue] = useState(() => {
try {
const value = window.localStorage.getItem(key)
// Check if the local storage already has any values,
// otherwise initialize it with the passed initialValue
return value ? JSON.parse(value) : initialValue
if (typeof window !== 'undefined') {
const item = window.localStorage.getItem(key);
// Vérifier si l'item existe et n'est pas undefined
return item !== null && item !== 'undefined'
? JSON.parse(item)
: initialValue;
}
return initialValue;
} catch (error) {
console.log(error)
console.error('Error reading from localStorage:', error);
return initialValue;
}
})
});
const setValue = value => {
useEffect(() => {
try {
// If the passed value is a callback function,
// then call it with the existing state.
const valueToStore = value instanceof Function ? value(state) : value
window.localStorage.setItem(key, JSON.stringify(valueToStore))
setState(value)
// Vérifier si la valeur n'est pas undefined avant de la stocker
if (typeof storedValue !== 'undefined') {
window.localStorage.setItem(key, JSON.stringify(storedValue));
} else {
window.localStorage.removeItem(key);
}
} catch (error) {
console.log(error)
console.error('Error writing to localStorage:', error);
}
}
}, [key, storedValue]);
return [state, setValue]
}
const setValue = (value) => {
try {
// Permettre à la valeur d'être une fonction
const valueToStore = value instanceof Function ? value(storedValue) : value;
setStoredValue(valueToStore);
} catch (error) {
console.error('Error updating localStorage value:', error);
}
};
export default useLocalStorage
return [storedValue, setValue];
};
export default useLocalStorage;