move to git.n39.euall books in the bag ...

This commit is contained in:
0ry5 2024-09-09 20:41:15 +02:00
commit 8cc2662092
64 changed files with 134252 additions and 0 deletions
frontend/src/shared/utils

View file

@ -0,0 +1,32 @@
import { useCallback, useContext } from "react";
import { AuthContext } from "../../App";
import { useQuery } from "@tanstack/react-query";
type UseAuthHook = { authenticated: boolean };
export const useAuth = (): UseAuthHook => {
const auth = useContext(AuthContext);
const authenticationCheck = useCallback(
async () =>
await fetch("/api/authenticate", {
method: "POST",
body: JSON.stringify({ password: "authCheck" }),
headers: {
"Content-Type": "application/json",
},
}).then((res) => {
auth.setAuthenticated?.(res.ok);
//react-query can't handle undefined without throwing a warning ...
return null;
}),
[auth]
);
useQuery({
queryFn: authenticationCheck,
queryKey: ["auth"],
});
return auth;
};