Moved useAuthStore from utils to service to break circular dependency: - utils was importing from service (supabase client, types) - service was importing from utils (useAuthStore) - Solution: moved useAuthStore and related storage utilities to service Changes: - Created libs/service/src/storage/ with cookies.ts and local-storage.ts - Moved use-auth-store.ts from utils/hooks to service/hooks/auth - Updated all 20+ files to import useAuthStore from service instead of utils - Removed useAuthStore export from utils - Added storage exports to service index This fixes the build error: "Could not execute command because the task graph has a circular dependency" 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
79 lines
1.9 KiB
TypeScript
79 lines
1.9 KiB
TypeScript
import { useForm } from 'react-hook-form';
|
|
import {
|
|
authLoginSchema,
|
|
TLoginRequest,
|
|
usePostLogin,
|
|
useAuthStore,
|
|
} from '@imphnen-frontend-service/service';
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
|
import { toast } from 'sonner';
|
|
import { useNavigate } from 'react-router';
|
|
import { useVerifyEmail } from './use-verify-email';
|
|
|
|
export const useLogin = () => {
|
|
const form = useForm<TLoginRequest>({
|
|
resolver: zodResolver(authLoginSchema),
|
|
mode: 'all',
|
|
});
|
|
|
|
const navigate = useNavigate();
|
|
const { mutate, isPending } = usePostLogin();
|
|
const { setLoading, setSession, clearSession } = useAuthStore();
|
|
|
|
const {
|
|
openVerifyModal,
|
|
showVerifyModal,
|
|
verifyForm,
|
|
onVerifySubmit,
|
|
closeVerifyModal,
|
|
isVerifying,
|
|
emailToVerify,
|
|
} = useVerifyEmail();
|
|
|
|
const onSubmit = form.handleSubmit((data) => {
|
|
setLoading(true);
|
|
mutate(data, {
|
|
onSuccess: (response) => {
|
|
toast.success('Login sukses');
|
|
|
|
if (response.data.token && response.data.user) {
|
|
setSession(response.data);
|
|
} else {
|
|
const { token, ...userData } = response.data;
|
|
|
|
const loginData = {
|
|
token,
|
|
userData,
|
|
};
|
|
setSession(loginData);
|
|
}
|
|
|
|
navigate(0);
|
|
},
|
|
onError: (error) => {
|
|
const errorMessage = error?.response?.data?.message;
|
|
|
|
if (errorMessage === 'Account not active, please verify your email') {
|
|
toast.error('Akun belum aktif, silakan verifikasi email Anda');
|
|
openVerifyModal(data.email);
|
|
} else {
|
|
toast.error(errorMessage ?? 'Terjadi Kesalahan yang tidak diketahui');
|
|
clearSession();
|
|
}
|
|
},
|
|
});
|
|
});
|
|
|
|
return {
|
|
form,
|
|
onSubmit,
|
|
isLoading: isPending,
|
|
showVerifyModal,
|
|
verifyForm,
|
|
onVerifySubmit,
|
|
closeVerifyModal,
|
|
isVerifying,
|
|
emailToVerify,
|
|
};
|
|
};
|