2020-09-04 05:59:35 -07:00
|
|
|
import { useAuth0 } from "@auth0/auth0-react";
|
|
|
|
|
|
|
|
function useCurrentUser() {
|
|
|
|
const { isLoading, isAuthenticated, user } = useAuth0();
|
|
|
|
|
|
|
|
if (isLoading || !isAuthenticated) {
|
2020-10-22 21:20:49 -07:00
|
|
|
return { id: null, username: null, isLoggedIn: false };
|
2020-09-04 05:59:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Users created correctly should have these attributes... but I'm
|
|
|
|
// coding defensively, because third-party integrations are always a
|
|
|
|
// bit of a thing, and I don't want failures to crash us!
|
|
|
|
const id = user.sub?.match(/^auth0\|impress-([0-9]+)$/)?.[1];
|
|
|
|
const username = user["https://oauth.impress-2020.openneo.net/username"];
|
|
|
|
|
2020-10-22 21:20:49 -07:00
|
|
|
return { id, username, isLoggedIn: true };
|
2020-09-04 05:59:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export default useCurrentUser;
|