diff --git a/front/src/service/session.ts b/front/src/service/session.ts index c0a6d59..3f9cd1d 100644 --- a/front/src/service/session.ts +++ b/front/src/service/session.ts @@ -1,21 +1,33 @@ import { useCallback, useEffect, useMemo, useState } from 'react'; -import { PartRight, RESTConfig, UserMe, UserResource } from '@/back-api'; +import { createListCollection } from '@chakra-ui/react'; +import { useEffectOnce } from 'react-use'; + +import { + PartRight, + RESTConfig, + UserMe, + UserResource, + setErrorApiGlobalCallback, +} from '@/back-api'; import { environment, getApiUrl } from '@/environment'; import { useServiceContext } from '@/service/ServiceContext'; import { SessionState } from '@/service/SessionState'; import { isBrowser } from '@/utils/layout'; import { parseToken } from '@/utils/sso'; -import { createListCollection } from '@chakra-ui/react'; const TOKEN_KEY = 'karusic-token-key-storage'; export const USERS_COLLECTION = createListCollection({ items: [ - { label: "admin", value: "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiIxIiwiYXBwbGljYXRpb24iOiJrYXJ1c2ljIiwiaXNzIjoiS2FyQXV0aCIsInJpZ2h0Ijp7ImthcnVzaWMiOnsiQURNSU4iOnRydWUsIlVTRVIiOnRydWV9fSwibG9naW4iOiJIZWVyb1l1aSIsImV4cCI6MTcyNDIwNjc5NCwiaWF0IjoxNzI0MTY2ODM0fQ.TEST_SIGNATURE_FOR_LOCAL_TEST_AND_TEST_E2E" }, - { label: "NO_USER", value: "svelte" }, + { + label: 'admin', + value: + 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiIxIiwiYXBwbGljYXRpb24iOiJrYXJ1c2ljIiwiaXNzIjoiS2FyQXV0aCIsInJpZ2h0Ijp7ImthcnVzaWMiOnsiQURNSU4iOnRydWUsIlVTRVIiOnRydWV9fSwibG9naW4iOiJIZWVyb1l1aSIsImV4cCI6MTcyNDIwNjc5NCwiaWF0IjoxNzI0MTY2ODM0fQ.TEST_SIGNATURE_FOR_LOCAL_TEST_AND_TEST_E2E', + }, + { label: 'NO_USER', value: 'svelte' }, ], -}) +}); export const USERS = { admin: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiIxIiwiYXBwbGljYXRpb24iOiJrYXJ1c2ljIiwiaXNzIjoiS2FyQXV0aCIsInJpZ2h0Ijp7ImthcnVzaWMiOnsiQURNSU4iOnRydWUsIlVTRVIiOnRydWV9fSwibG9naW4iOiJIZWVyb1l1aSIsImV4cCI6MTcyNDIwNjc5NCwiaWF0IjoxNzI0MTY2ODM0fQ.TEST_SIGNATURE_FOR_LOCAL_TEST_AND_TEST_E2E', @@ -57,20 +69,26 @@ export const useSessionServiceWrapped = (): SessionServiceProps => { const [state, setState] = useState(SessionState.NO_USER); const [config, setConfig] = useState(undefined); + useEffectOnce(() => { + setErrorApiGlobalCallback((response: Response) => { + if (response.status == 401) { + console.error('Detect 401 error ==> remove token'); + clearToken(); + } + }); + }); + const updateRight = useCallback(() => { + console.log('update right...'); if (isBrowser) { console.log('Detect a new token...'); - setState(SessionState.NO_USER); - setConfig(undefined); + if (token === undefined) { console.log(` ==> No User`); setState(SessionState.NO_USER); + setConfig(undefined); localStorage.removeItem(TOKEN_KEY); - } else if (token === '__LOGOUT__') { - console.log(` ==> disconnection: ${token}`); - setState(SessionState.DISCONNECT); - localStorage.removeItem(TOKEN_KEY); - } else if (!['__LOGOUT__', '__FAIL__', '__CANCEL__'].includes(token)) { + } else { console.log(' ==> Login ... (try to keep right)'); setState(SessionState.CONNECTING); localStorage.setItem(TOKEN_KEY, token); @@ -84,6 +102,7 @@ export const useSessionServiceWrapped = (): SessionServiceProps => { }) .catch((error) => { setState(SessionState.CONNECTION_FAIL); + setConfig(undefined); //console.log(` ==> Fail to get right: '${error}'`); localStorage.removeItem(TOKEN_KEY); }); @@ -91,7 +110,10 @@ export const useSessionServiceWrapped = (): SessionServiceProps => { } }, [localStorage, parseToken, token]); const setTokenLocal = useCallback( - (token: string) => { + (token?: string) => { + if (token ? token.startsWith('__') : false) { + token = undefined; + } setToken(token); updateRight(); },