move to git.n39.euall books in the bag ...
This commit is contained in:
commit
8cc2662092
64 changed files with 134252 additions and 0 deletions
frontend/src/shared/utils
32
frontend/src/shared/utils/useAuthentication.ts
Normal file
32
frontend/src/shared/utils/useAuthentication.ts
Normal 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;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue