261012_01_FST: first commit
This commit is contained in:
41
src/query/auth.ts
Normal file
41
src/query/auth.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
"use client";
|
||||
|
||||
import type { LoginRequest, LoginUser } from "@kri/common";
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { useAuth } from "@/hooks/useAuth";
|
||||
import { nextApi } from "@/lib/http-client";
|
||||
|
||||
export function useLoginMutation() {
|
||||
const { setFromLoginResponse } = useAuth();
|
||||
return useMutation({
|
||||
mutationFn: (request: LoginRequest) => nextApi.post<LoginUser>("/login", request, {
|
||||
skipUnauthorizedHandler: true,
|
||||
skipForbiddenHandler: true,
|
||||
skipResponseErrorHandler: true,
|
||||
}),
|
||||
onSuccess: setFromLoginResponse,
|
||||
});
|
||||
}
|
||||
|
||||
export function useLogoutMutation() {
|
||||
const queryClient = useQueryClient();
|
||||
const { auth, logout } = useAuth();
|
||||
return useMutation({
|
||||
mutationFn: async () => {
|
||||
const suffix = auth.user?.userId ? `/${encodeURIComponent(auth.user.userId)}` : "";
|
||||
try {
|
||||
await nextApi.post(`/logout${suffix}`, null, {
|
||||
skipUnauthorizedHandler: true,
|
||||
skipForbiddenHandler: true,
|
||||
skipResponseErrorHandler: true,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
},
|
||||
onSettled: () => {
|
||||
logout();
|
||||
queryClient.clear();
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user