86 lines
2.5 KiB
TypeScript
86 lines
2.5 KiB
TypeScript
|
|
import { useMutation } from "@tanstack/react-query";
|
||
|
|
import type { NPEDFieldType } from "../types/types";
|
||
|
|
import { useNPEDContext } from "../context/NPEDUserContext";
|
||
|
|
|
||
|
|
const base_url = import.meta.env.VITE_OUTSIDE_BASEURL;
|
||
|
|
|
||
|
|
async function signIn(loginDetails: NPEDFieldType) {
|
||
|
|
const { frontId, rearId, username, password, clientId } = loginDetails;
|
||
|
|
|
||
|
|
const NPEDLoginURLFront = `${base_url}/update-config?id=${frontId}`;
|
||
|
|
const NPEDLoginURLRear = `${base_url}/update-config?id=${rearId}`;
|
||
|
|
|
||
|
|
const frontCameraPayload = {
|
||
|
|
id: frontId,
|
||
|
|
fields: [
|
||
|
|
{ property: "propEnabled", value: true },
|
||
|
|
{ property: "propUsername", value: username },
|
||
|
|
{ property: "propPassword", value: password },
|
||
|
|
{ property: "propClientID", value: clientId },
|
||
|
|
],
|
||
|
|
};
|
||
|
|
|
||
|
|
const rearCameraPayload = {
|
||
|
|
id: rearId,
|
||
|
|
fields: [
|
||
|
|
{ property: "propEnabled", value: true },
|
||
|
|
{ property: "propUsername", value: username },
|
||
|
|
{ property: "propPassword", value: password },
|
||
|
|
{ property: "propClientID", value: clientId },
|
||
|
|
],
|
||
|
|
};
|
||
|
|
|
||
|
|
const frontCameraResponse = await fetch(NPEDLoginURLFront, {
|
||
|
|
method: "POST",
|
||
|
|
body: JSON.stringify(frontCameraPayload),
|
||
|
|
});
|
||
|
|
|
||
|
|
const rearCameraResponse = await fetch(NPEDLoginURLRear, {
|
||
|
|
method: "POST",
|
||
|
|
body: JSON.stringify(rearCameraPayload),
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!frontCameraResponse.ok) throw new Error("cannot reach NPED endpoint");
|
||
|
|
if (!rearCameraResponse.ok) throw new Error("cannot reach NPED endpoint");
|
||
|
|
|
||
|
|
console.log(frontCameraResponse);
|
||
|
|
console.log(rearCameraResponse);
|
||
|
|
return {
|
||
|
|
frontResponse: frontCameraResponse.json(),
|
||
|
|
rearResponse: rearCameraResponse.json(),
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
async function signOut() {
|
||
|
|
const response = await fetch(url, { method: "POST" });
|
||
|
|
if (!response.ok) throw new Error("cannot reach NPED sign out endpoint");
|
||
|
|
return response.json();
|
||
|
|
}
|
||
|
|
|
||
|
|
export const useNPEDAuth = () => {
|
||
|
|
const { setUser } = useNPEDContext();
|
||
|
|
|
||
|
|
const signInMutation = useMutation({
|
||
|
|
mutationKey: ["NPEDSignin"],
|
||
|
|
mutationFn: signIn,
|
||
|
|
onSuccess: async (data) => setUser(await data.frontResponse),
|
||
|
|
});
|
||
|
|
|
||
|
|
const signOutMutation = useMutation({
|
||
|
|
mutationKey: ["auth", "NPEDSignOut"],
|
||
|
|
mutationFn: signOut,
|
||
|
|
onSuccess: () => setUser(null),
|
||
|
|
});
|
||
|
|
|
||
|
|
return {
|
||
|
|
signIn: signInMutation.mutate,
|
||
|
|
signInAsync: signInMutation.mutateAsync,
|
||
|
|
isPending: signInMutation.isPending,
|
||
|
|
isError: signInMutation.isError,
|
||
|
|
error: signInMutation.error,
|
||
|
|
data: signInMutation.data,
|
||
|
|
|
||
|
|
signOut: signOutMutation.mutate,
|
||
|
|
};
|
||
|
|
};
|