2023-10-04 17:26:47 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { useCookies } from 'react-cookie';
|
|
|
|
import { logout } from '../api/authentication';
|
|
|
|
|
|
|
|
function useClientInfos() {
|
2023-10-07 13:37:40 +00:00
|
|
|
const [cookies] = useCookies(['client-infos', 'jwttoken']);
|
|
|
|
|
|
|
|
if(cookies['jwttoken'] === undefined) {
|
|
|
|
// probably the demo or new install
|
|
|
|
return {
|
|
|
|
nickname: "",
|
|
|
|
role: "2"
|
|
|
|
}
|
|
|
|
}
|
2023-10-04 17:26:47 +00:00
|
|
|
|
|
|
|
let clientInfos = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Try to parse the cookie into a JavaScript object
|
|
|
|
clientInfos = cookies['client-infos'].split(',');
|
2023-10-07 13:37:40 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
nickname: clientInfos[0],
|
|
|
|
role: clientInfos[1]
|
|
|
|
};
|
2023-10-04 17:26:47 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error('Error parsing client-infos cookie:', error);
|
|
|
|
logout();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
useClientInfos
|
|
|
|
};
|