2021-03-11 13:38:35 -08:00
|
|
|
import Cookies from 'js-cookie';
|
|
|
|
import { CLIENT_AUTH_COOKIE } from '../conf/Global';
|
2021-02-20 12:01:42 -08:00
|
|
|
import { useGetUserQuery } from '../graphql/user.generated';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch a CorpUser object corresponding to the currently authenticated user.
|
|
|
|
*/
|
|
|
|
export function useGetAuthenticatedUser() {
|
2021-05-03 07:51:46 -07:00
|
|
|
const userUrn = Cookies.get(CLIENT_AUTH_COOKIE);
|
|
|
|
if (!userUrn) {
|
|
|
|
throw new Error('Could not find logged in user.');
|
|
|
|
}
|
|
|
|
const { data, error } = useGetUserQuery({ variables: { urn: userUrn } });
|
|
|
|
if (error) {
|
|
|
|
throw new Error(`Could not fetch logged in user from cache. + ${error.message}`);
|
|
|
|
}
|
|
|
|
return data?.corpUser;
|
2021-02-20 12:01:42 -08:00
|
|
|
}
|