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

@ -1,3 +1,11 @@
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