mirror of
https://github.com/Nezumi-2711/react-training.git
synced 2026-09-22 13:38:51 +00:00
33 lines
791 B
TypeScript
33 lines
791 B
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
import { useNavigate } from 'react-router-dom';
|
|
import toast from 'react-hot-toast';
|
|
|
|
// Services
|
|
import { login as loginFn } from '@src/services/authenticationService';
|
|
|
|
/**
|
|
* Login web
|
|
* @returns The login function and isPending boolean
|
|
*/
|
|
const useLogin = () => {
|
|
const queryClient = useQueryClient();
|
|
const navigate = useNavigate();
|
|
|
|
const { mutate: login, isPending } = useMutation({
|
|
mutationFn: loginFn,
|
|
onSuccess: (account) => {
|
|
queryClient.setQueryData(['account'], account.user);
|
|
|
|
navigate('/');
|
|
},
|
|
onError: (err) => {
|
|
console.error(err.message);
|
|
toast.error('Email or password not correct!');
|
|
},
|
|
});
|
|
|
|
return { login, isPending };
|
|
};
|
|
|
|
export { useLogin };
|