-
Any proper way to prevent triggering a query if I know in the client beforehand that the user can't make the query? My current code import { useFirestore, useFirestoreCollectionData } from "reactfire";
import { Project } from "types";
import { useUserRole } from "hooks";
export const adminArray = new Set([
"admin",
"moderator",
"superUser",
]) as Set<UserRole>;
export default function usePrivateProjects() {
const role = useUserRole();
const query = adminArray.has(role) ? "projects" : " ";
const projectsRef = useFirestore().collection(query);
return useFirestoreCollectionData<Project>(projectsRef, {
initialData: [],
idField: "id",
});
} |
Beta Was this translation helpful? Give feedback.
Answered by
eddyhdzg
Aug 29, 2021
Replies: 1 comment 3 replies
-
I solved it by using useSigninCheck as in #368 (comment) and a Wrapper similar to the AuthWrapper in the example folder. A Simple hook to check if user is an admin import { useSigninCheck } from "reactfire";
import { UserRole } from "types";
import { adminArray } from "constant";
export default function useIsAdmin() {
const { status, data: signInCheckResult } = useSigninCheck({
validateCustomClaims: (userClaims) => {
return {
hasRequiredClaims: adminArray.has(userClaims.role as UserRole),
errors: {},
};
},
});
return { status, signInCheckResult };
} AdminWrapper import { CenterLoader, MessagePaper } from "components";
import { useIsAdmin } from "hooks";
export default function AdminWrapper({
children,
fallback = (
<MessagePaper message="You need to be an admin to use this route" />
),
}: React.PropsWithChildren<{
fallback?: JSX.Element;
}>): JSX.Element {
const { status, signInCheckResult } = useIsAdmin();
if (!children) {
throw new Error("Children must be provided");
}
if (status === "loading") {
return <CenterLoader />;
} else if (
signInCheckResult?.signedIn &&
signInCheckResult?.hasRequiredClaims
) {
return children as JSX.Element;
}
return fallback;
} |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
jhuleatt
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I solved it by using useSigninCheck as in #368 (comment) and a Wrapper similar to the AuthWrapper in the example folder.
A Simple hook to check if user is an admin
AdminWrapper