fix(striker-ui): align cookie jar with API

main
Tsu-ba-me 1 year ago
parent 0fad937554
commit 3ee7a41895
  1. 16
      striker-ui/hooks/useCookieJar.ts
  2. 10
      striker-ui/types/CookieJar.d.ts

@ -5,7 +5,8 @@ import useIsFirstRender from './useIsFirstRender';
const useCookieJar = (): { const useCookieJar = (): {
cookieJar: CookieJar; cookieJar: CookieJar;
getCookie: <T>(key: string) => T | undefined; getCookie: <T>(key: string) => T | undefined;
getSessionUser: () => SessionUser | undefined; getSession: () => SessionCookie | undefined;
getSessionUser: () => SessionCookieUser | undefined;
} => { } => {
const isFirstRender = useIsFirstRender(); const isFirstRender = useIsFirstRender();
@ -17,11 +18,13 @@ const useCookieJar = (): {
[cookieJar], [cookieJar],
); );
const getSessionUser = useCallback( const getSession = useCallback(
() => getCookie<SessionUser>('user'), () => getCookie<SessionCookie>('session'),
[getCookie], [getCookie],
); );
const getSessionUser = useCallback(() => getSession()?.user, [getSession]);
useEffect(() => { useEffect(() => {
if (isFirstRender) { if (isFirstRender) {
const lines = document.cookie.split(/\s*;\s*/); const lines = document.cookie.split(/\s*;\s*/);
@ -52,7 +55,12 @@ const useCookieJar = (): {
} }
}, [isFirstRender]); }, [isFirstRender]);
return { cookieJar, getCookie, getSessionUser }; return {
cookieJar,
getCookie,
getSession,
getSessionUser,
};
}; };
export default useCookieJar; export default useCookieJar;

@ -1,3 +1,11 @@
type CookieJar = Record<string, unknown>; type CookieJar = Record<string, unknown>;
type SessionUser = { name: string; uuid: string }; type SessionCookieUser = {
name: string;
uuid: string;
};
type SessionCookie = {
expires: string;
user: SessionCookieUser;
};

Loading…
Cancel
Save