Add authentification

This commit is contained in:
2026-02-17 00:54:36 +01:00
parent ab98ba81c8
commit a8c8c489da
31 changed files with 1118 additions and 451 deletions

View File

@@ -13,7 +13,13 @@ import type {
ProductorCreate,
ProductorEditPayload,
} from "@/services/resources/productors";
import type { User, UserCreate, UserEditPayload } from "@/services/resources/users";
import type {
Role,
User,
UserCreate,
UserEditPayload,
UserLogged,
} from "@/services/resources/users";
import type { Product, ProductCreate, ProductEditPayload } from "./resources/products";
import type { Contract, ContractCreate } from "./resources/contracts";
import { notifications } from "@mantine/notifications";
@@ -24,9 +30,9 @@ export function useGetShipments(filters?: URLSearchParams): UseQueryResult<Shipm
return useQuery<Shipment[]>({
queryKey: ["shipments", queryString],
queryFn: () =>
fetch(`${Config.backend_uri}/shipments${filters ? `?${queryString}` : ""}`).then(
(res) => res.json(),
),
fetch(`${Config.backend_uri}/shipments${filters ? `?${queryString}` : ""}`, {
credentials: "include",
}).then((res) => res.json()),
});
}
@@ -36,7 +42,10 @@ export function useGetShipment(
): UseQueryResult<Shipment, Error> {
return useQuery<Shipment>({
queryKey: ["shipment"],
queryFn: () => fetch(`${Config.backend_uri}/shipments/${id}`).then((res) => res.json()),
queryFn: () =>
fetch(`${Config.backend_uri}/shipments/${id}`, {
credentials: "include",
}).then((res) => res.json()),
enabled: !!id,
...options,
});
@@ -49,6 +58,7 @@ export function useCreateShipment() {
mutationFn: (newShipment: ShipmentCreate) => {
return fetch(`${Config.backend_uri}/shipments`, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
@@ -65,7 +75,9 @@ export function useCreateShipment() {
onError: (error) => {
notifications.show({
title: t("error", { capfirst: true }),
message: error?.message || t(`error create`, { capfirst: true, entity: t("of the shipment") }),
message:
error?.message ||
t(`error create`, { capfirst: true, entity: t("of the shipment") }),
color: "red",
});
},
@@ -79,6 +91,7 @@ export function useEditShipment() {
mutationFn: ({ shipment, id }: ShipmentEditPayload) => {
return fetch(`${Config.backend_uri}/shipments/${id}`, {
method: "PUT",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
@@ -95,7 +108,9 @@ export function useEditShipment() {
onError: (error) => {
notifications.show({
title: t("error", { capfirst: true }),
message: error?.message || t(`error edit`, { capfirst: true, entity: t("of the shipment") }),
message:
error?.message ||
t(`error edit`, { capfirst: true, entity: t("of the shipment") }),
color: "red",
});
},
@@ -108,6 +123,7 @@ export function useDeleteShipment() {
mutationFn: (id: number) => {
return fetch(`${Config.backend_uri}/shipments/${id}`, {
method: "DELETE",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
@@ -123,7 +139,9 @@ export function useDeleteShipment() {
onError: (error) => {
notifications.show({
title: t("error", { capfirst: true }),
message: error?.message || t(`error delete`, { capfirst: true, entity: t("of the contract") }),
message:
error?.message ||
t(`error delete`, { capfirst: true, entity: t("of the contract") }),
color: "red",
});
},
@@ -135,9 +153,9 @@ export function useGetProductors(filters?: URLSearchParams): UseQueryResult<Prod
return useQuery<Productor[]>({
queryKey: ["productors", queryString],
queryFn: () =>
fetch(`${Config.backend_uri}/productors${filters ? `?${queryString}` : ""}`).then(
(res) => res.json(),
),
fetch(`${Config.backend_uri}/productors${filters ? `?${queryString}` : ""}`, {
credentials: "include",
}).then((res) => res.json()),
});
}
@@ -147,7 +165,10 @@ export function useGetProductor(
) {
return useQuery<Productor>({
queryKey: ["productor"],
queryFn: () => fetch(`${Config.backend_uri}/productors/${id}`).then((res) => res.json()),
queryFn: () =>
fetch(`${Config.backend_uri}/productors/${id}`, {
credentials: "include",
}).then((res) => res.json()),
enabled: !!id,
...options,
});
@@ -160,6 +181,7 @@ export function useCreateProductor() {
mutationFn: (newProductor: ProductorCreate) => {
return fetch(`${Config.backend_uri}/productors`, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
@@ -176,7 +198,9 @@ export function useCreateProductor() {
onError: (error) => {
notifications.show({
title: t("error", { capfirst: true }),
message: error?.message || t(`error create`, { capfirst: true, entity: t("of the productor") }),
message:
error?.message ||
t(`error create`, { capfirst: true, entity: t("of the productor") }),
color: "red",
});
},
@@ -190,6 +214,7 @@ export function useEditProductor() {
mutationFn: ({ productor, id }: ProductorEditPayload) => {
return fetch(`${Config.backend_uri}/productors/${id}`, {
method: "PUT",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
@@ -206,7 +231,9 @@ export function useEditProductor() {
onError: (error) => {
notifications.show({
title: t("error", { capfirst: true }),
message: error?.message || t(`error edit`, { capfirst: true, entity: t("of the productor") }),
message:
error?.message ||
t(`error edit`, { capfirst: true, entity: t("of the productor") }),
color: "red",
});
},
@@ -219,6 +246,7 @@ export function useDeleteProductor() {
mutationFn: (id: number) => {
return fetch(`${Config.backend_uri}/productors/${id}`, {
method: "DELETE",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
@@ -234,7 +262,9 @@ export function useDeleteProductor() {
onError: (error) => {
notifications.show({
title: t("error", { capfirst: true }),
message: error?.message || t(`error delete`, { capfirst: true, entity: t("of the productor") }),
message:
error?.message ||
t(`error delete`, { capfirst: true, entity: t("of the productor") }),
color: "red",
});
},
@@ -247,7 +277,10 @@ export function useGetForm(
) {
return useQuery<Form>({
queryKey: ["form"],
queryFn: () => fetch(`${Config.backend_uri}/forms/${id}`).then((res) => res.json()),
queryFn: () =>
fetch(`${Config.backend_uri}/forms/${id}`, {
credentials: "include",
}).then((res) => res.json()),
enabled: !!id,
...options,
});
@@ -258,9 +291,9 @@ export function useGetForms(filters?: URLSearchParams): UseQueryResult<Form[], E
return useQuery<Form[]>({
queryKey: ["forms", queryString],
queryFn: () =>
fetch(`${Config.backend_uri}/forms${filters ? `?${queryString}` : ""}`).then((res) =>
res.json(),
),
fetch(`${Config.backend_uri}/forms${filters ? `?${queryString}` : ""}`, {
credentials: "include",
}).then((res) => res.json()),
});
}
@@ -271,6 +304,7 @@ export function useCreateForm() {
mutationFn: (newForm: FormCreate) => {
return fetch(`${Config.backend_uri}/forms`, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
@@ -289,6 +323,7 @@ export function useDeleteForm() {
mutationFn: (id: number) => {
return fetch(`${Config.backend_uri}/forms/${id}`, {
method: "DELETE",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
@@ -304,7 +339,9 @@ export function useDeleteForm() {
onError: (error) => {
notifications.show({
title: t("error", { capfirst: true }),
message: error?.message || t(`error delete`, { capfirst: true, entity: t("of the form") }),
message:
error?.message ||
t(`error delete`, { capfirst: true, entity: t("of the form") }),
color: "red",
});
},
@@ -318,6 +355,7 @@ export function useEditForm() {
mutationFn: ({ id, form }: FormEditPayload) => {
return fetch(`${Config.backend_uri}/forms/${id}`, {
method: "PUT",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
@@ -334,7 +372,8 @@ export function useEditForm() {
onError: (error) => {
notifications.show({
title: t("error", { capfirst: true }),
message: error?.message || t(`error edit`, { capfirst: true, entity: t("of the form") }),
message:
error?.message || t(`error edit`, { capfirst: true, entity: t("of the form") }),
color: "red",
});
},
@@ -347,7 +386,10 @@ export function useGetProduct(
) {
return useQuery<Product>({
queryKey: ["product"],
queryFn: () => fetch(`${Config.backend_uri}/products/${id}`).then((res) => res.json()),
queryFn: () =>
fetch(`${Config.backend_uri}/products/${id}`, {
credentials: "include",
}).then((res) => res.json()),
enabled: !!id,
...options,
});
@@ -358,9 +400,9 @@ export function useGetProducts(filters?: URLSearchParams): UseQueryResult<Produc
return useQuery<Product[]>({
queryKey: ["products", queryString],
queryFn: () =>
fetch(`${Config.backend_uri}/products${filters ? `?${queryString}` : ""}`).then((res) =>
res.json(),
),
fetch(`${Config.backend_uri}/products${filters ? `?${queryString}` : ""}`, {
credentials: "include",
}).then((res) => res.json()),
});
}
@@ -371,6 +413,7 @@ export function useCreateProduct() {
mutationFn: (newProduct: ProductCreate) => {
return fetch(`${Config.backend_uri}/products`, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
@@ -387,7 +430,9 @@ export function useCreateProduct() {
onError: (error) => {
notifications.show({
title: t("error", { capfirst: true }),
message: error?.message || t(`error create`, { capfirst: true, entity: t("of the productor") }),
message:
error?.message ||
t(`error create`, { capfirst: true, entity: t("of the productor") }),
color: "red",
});
},
@@ -400,6 +445,7 @@ export function useDeleteProduct() {
mutationFn: (id: number) => {
return fetch(`${Config.backend_uri}/products/${id}`, {
method: "DELETE",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
@@ -415,7 +461,9 @@ export function useDeleteProduct() {
onError: (error) => {
notifications.show({
title: t("error", { capfirst: true }),
message: error?.message || t(`error delete`, { capfirst: true, entity: t("of the product") }),
message:
error?.message ||
t(`error delete`, { capfirst: true, entity: t("of the product") }),
color: "red",
});
},
@@ -429,6 +477,7 @@ export function useEditProduct() {
mutationFn: ({ id, product }: ProductEditPayload) => {
return fetch(`${Config.backend_uri}/products/${id}`, {
method: "PUT",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
@@ -445,7 +494,9 @@ export function useEditProduct() {
onError: (error) => {
notifications.show({
title: t("error", { capfirst: true }),
message: error?.message || t(`error edit`, { capfirst: true, entity: t("of the product") }),
message:
error?.message ||
t(`error edit`, { capfirst: true, entity: t("of the product") }),
color: "red",
});
},
@@ -458,7 +509,10 @@ export function useGetUser(
) {
return useQuery<User>({
queryKey: ["user"],
queryFn: () => fetch(`${Config.backend_uri}/users/${id}`).then((res) => res.json()),
queryFn: () =>
fetch(`${Config.backend_uri}/users/${id}`, {
credentials: "include",
}).then((res) => res.json()),
enabled: !!id,
...options,
});
@@ -469,9 +523,9 @@ export function useGetUsers(filters?: URLSearchParams): UseQueryResult<User[], E
return useQuery<User[]>({
queryKey: ["users", queryString],
queryFn: () =>
fetch(`${Config.backend_uri}/users${filters ? `?${queryString}` : ""}`).then((res) =>
res.json(),
),
fetch(`${Config.backend_uri}/users${filters ? `?${queryString}` : ""}`, {
credentials: "include",
}).then((res) => res.json()),
});
}
@@ -482,6 +536,7 @@ export function useCreateUser() {
mutationFn: (newUser: UserCreate) => {
return fetch(`${Config.backend_uri}/users`, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
@@ -498,7 +553,9 @@ export function useCreateUser() {
onError: (error) => {
notifications.show({
title: t("error", { capfirst: true }),
message: error?.message || t(`error create`, { capfirst: true, entity: t("of the user") }),
message:
error?.message ||
t(`error create`, { capfirst: true, entity: t("of the user") }),
color: "red",
});
},
@@ -511,6 +568,7 @@ export function useDeleteUser() {
mutationFn: (id: number) => {
return fetch(`${Config.backend_uri}/users/${id}`, {
method: "DELETE",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
@@ -526,7 +584,9 @@ export function useDeleteUser() {
onError: (error) => {
notifications.show({
title: t("error", { capfirst: true }),
message: error?.message || t(`error delete`, { capfirst: true, entity: t("of the user") }),
message:
error?.message ||
t(`error delete`, { capfirst: true, entity: t("of the user") }),
color: "red",
});
},
@@ -540,6 +600,7 @@ export function useEditUser() {
mutationFn: ({ id, user }: UserEditPayload) => {
return fetch(`${Config.backend_uri}/users/${id}`, {
method: "PUT",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
@@ -556,7 +617,8 @@ export function useEditUser() {
onError: (error) => {
notifications.show({
title: t("error", { capfirst: true }),
message: error?.message || t(`error edit`, { capfirst: true, entity: t("of the user") }),
message:
error?.message || t(`error edit`, { capfirst: true, entity: t("of the user") }),
color: "red",
});
},
@@ -568,9 +630,9 @@ export function useGetContracts(filters?: URLSearchParams): UseQueryResult<Contr
return useQuery<Contract[]>({
queryKey: ["contracts", queryString],
queryFn: () =>
fetch(`${Config.backend_uri}/contracts${filters ? `?${queryString}` : ""}`).then(
(res) => res.json(),
),
fetch(`${Config.backend_uri}/contracts${filters ? `?${queryString}` : ""}`, {
credentials: "include",
}).then((res) => res.json()),
});
}
@@ -580,28 +642,68 @@ export function useGetContract(
) {
return useQuery<Contract>({
queryKey: ["contract"],
queryFn: () => fetch(`${Config.backend_uri}/contracts/${id}`).then((res) => res.json()),
queryFn: () =>
fetch(`${Config.backend_uri}/contracts/${id}`, {
credentials: "include",
}).then((res) => res.json()),
enabled: !!id,
...options,
});
}
export function useGetContractFile() {
return useMutation({
mutationFn: async (id: number) => {
const res = await fetch(`${Config.backend_uri}/contracts/${id}/file`, {
credentials: "include",
}).then((res) => res);
export function useGetContractFile(
id?: number,
) {
return useQuery({
queryKey: ["contract"],
queryFn: () => fetch(`${Config.backend_uri}/contracts/${id}/file`)
.then(async (res) => {
if (!res.ok) throw new Error();
const blob = await res.blob();
const disposition = res.headers.get("Content-Disposition");
const filename = disposition && disposition?.includes("filename=") ?
disposition.split("filname=")[1].replace(/"/g, "") :
`contract_${id}.pdf`
return {blob, filename};
}),
enabled: !!id,
const filename =
disposition && disposition?.includes("filename=")
? disposition.split("filename=")[1].replace(/"/g, "")
: `contract_${id}.pdf`;
console.log(disposition);
return { blob, filename };
},
onSuccess: ({ blob, filename }) => {
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = filename;
link.click();
URL.revokeObjectURL(url);
},
});
}
export function useGetAllContractFile() {
return useMutation({
mutationFn: async (form_id: number) => {
const res = await fetch(`${Config.backend_uri}/contracts/${form_id}/files`, {
credentials: "include",
}).then((res) => res);
if (!res.ok) throw new Error();
const blob = await res.blob();
const disposition = res.headers.get("Content-Disposition");
const filename =
disposition && disposition?.includes("filename=")
? disposition.split("filename=")[1].replace(/"/g, "")
: `contract_${form_id}.zip`;
console.log(disposition);
return { blob, filename };
},
onSuccess: ({ blob, filename }) => {
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = filename;
link.click();
URL.revokeObjectURL(url);
},
});
}
@@ -612,6 +714,7 @@ export function useCreateContract() {
mutationFn: (newContract: ContractCreate) => {
return fetch(`${Config.backend_uri}/contracts`, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
@@ -636,6 +739,7 @@ export function useDeleteContract() {
mutationFn: (id: number) => {
return fetch(`${Config.backend_uri}/contracts/${id}`, {
method: "DELETE",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
@@ -651,9 +755,54 @@ export function useDeleteContract() {
onError: (error) => {
notifications.show({
title: t("error", { capfirst: true }),
message: error?.message || t(`error delete`, { capfirst: true, entity: t("of the contract") }),
message:
error?.message ||
t(`error delete`, { capfirst: true, entity: t("of the contract") }),
color: "red",
});
},
});
}
export function useGetRoles(filters?: URLSearchParams): UseQueryResult<Role[], Error> {
const queryString = filters?.toString();
return useQuery<Role[]>({
queryKey: ["roles", queryString],
queryFn: () =>
fetch(`${Config.backend_uri}/users/roles${filters ? `?${queryString}` : ""}`, {
credentials: "include",
}).then((res) => res.json()),
});
}
export function useCurrentUser() {
return useQuery<UserLogged>({
queryKey: ["currentUser"],
queryFn: () => {
return fetch(`${Config.backend_uri}/auth/user/me`, {
credentials: "include",
}).then((res) => res.json());
},
retry: false,
refetchOnWindowFocus: false,
});
}
export function useLogoutUser() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: () => {
return fetch(`${Config.backend_uri}/auth/logout`, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
}).then((res) => res.json());
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["currentUser"] });
},
});
}