2021-03-11 13:38:35 -08:00
|
|
|
import Cookies from 'js-cookie';
|
|
|
|
import { CLIENT_AUTH_COOKIE } from '../conf/Global';
|
2021-09-02 19:05:13 -07:00
|
|
|
import { useGetMeQuery } from '../graphql/me.generated';
|
2021-02-20 12:01:42 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch a CorpUser object corresponding to the currently authenticated user.
|
|
|
|
*/
|
2022-05-30 00:26:07 -04:00
|
|
|
export function useGetAuthenticatedUser(skip?: boolean) {
|
2021-05-03 07:51:46 -07:00
|
|
|
const userUrn = Cookies.get(CLIENT_AUTH_COOKIE);
|
2022-10-07 14:08:43 -07:00
|
|
|
const { data, error } = useGetMeQuery({ skip: skip || !userUrn, fetchPolicy: 'cache-and-network' });
|
2021-05-03 07:51:46 -07:00
|
|
|
if (error) {
|
2021-06-03 13:24:33 -07:00
|
|
|
console.error(`Could not fetch logged in user from cache. + ${error.message}`);
|
2021-05-03 07:51:46 -07:00
|
|
|
}
|
2021-09-02 19:05:13 -07:00
|
|
|
return data?.me;
|
2021-02-20 12:01:42 -08:00
|
|
|
}
|
2022-03-14 14:07:49 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch an urn corresponding to the authenticated user.
|
|
|
|
*/
|
|
|
|
export function useGetAuthenticatedUserUrn() {
|
|
|
|
const userUrn = Cookies.get(CLIENT_AUTH_COOKIE);
|
|
|
|
if (!userUrn) {
|
|
|
|
throw new Error('Could not find logged in user.');
|
|
|
|
}
|
|
|
|
return userUrn;
|
|
|
|
}
|