cos tam dziala

This commit is contained in:
Patryk Koreń
2026-07-09 20:14:56 +02:00
parent ff707dd762
commit 65303e655b
18 changed files with 235 additions and 153 deletions

View File

@@ -0,0 +1,31 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
export function useLogin() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (data: {
username: string;
password: string;
}) => {
const res = await fetch(`${import.meta.env.VITE_API_URL}/auth/login`, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
if (!res.ok) {
throw new Error("Invalid credentials");
}
return res.json();
},
onSuccess(data) {
queryClient.setQueryData(["me"], data.user);
},
});
}

View File

@@ -0,0 +1,18 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
export function useLogout() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: async () => {
await fetch(`${import.meta.env.VITE_API_URL}/auth/logout`, {
method: "POST",
credentials: "include",
});
},
onSuccess() {
queryClient.setQueryData(["me"], null);
},
});
}

23
front/src/hooks/useMe.ts Normal file
View File

@@ -0,0 +1,23 @@
import { useQuery } from "@tanstack/react-query";
export function useMe() {
return useQuery<User>({
queryKey: ["me"],
queryFn: async () => {
const res = await fetch(`${import.meta.env.VITE_API_URL}/auth/me`, {
credentials: "include",
});
if (res.status === 401) {
return null;
}
if (!res.ok) {
throw new Error("Failed to get current user");
}
return res.json();
},
});
}

View File

@@ -0,0 +1,18 @@
import { useMutation } from "@tanstack/react-query";
export function useCreateClip() {
return useMutation({
mutationFn: async () => {
const res = await fetch(`${import.meta.env.VITE_API_URL}/protected/createclip`, {
method: "POST",
credentials: "include",
});
if (!res.ok) {
throw new Error("Invalid credentials");
}
return res.json();
},
});
}

View File

@@ -0,0 +1,18 @@
import { useMutation } from "@tanstack/react-query";
export function useRefreshFiles() {
return useMutation({
mutationFn: async () => {
const res = await fetch(`${import.meta.env.VITE_API_URL}/protected/refreshfiles`, {
method: "POST",
credentials: "include",
});
if (!res.ok) {
throw new Error("Invalid credentials");
}
return res.json();
},
});
}