25 lines
663 B
TypeScript
25 lines
663 B
TypeScript
import type { FormData } from '../lib/types';
|
|
import { redirect } from '@/components/Form/lib/utils';
|
|
import { ERROR_INVALID_CREDENTIALS } from '@/constants/errors';
|
|
import { FormStateContext } from '@/context/form-state';
|
|
import axios from 'axios';
|
|
import { useContext } from 'react';
|
|
|
|
export function useLogin() {
|
|
const { dispatch } = useContext(FormStateContext);
|
|
|
|
function handleLogin(data: FormData) {
|
|
return axios
|
|
.post('/login', data)
|
|
.then(() => redirect())
|
|
.catch(() =>
|
|
dispatch({
|
|
payload: { error: ERROR_INVALID_CREDENTIALS },
|
|
type: 'set-error',
|
|
})
|
|
);
|
|
}
|
|
|
|
return { handleLogin };
|
|
}
|