feat: gacha auth integration (#32)
* feat(gacha): register and login integration * fix: navbar and gacha landing page - display fullname when user authenticated - fix styling and responsive * fix: minor fix UI
This commit is contained in:
@@ -2,9 +2,13 @@ import { useForm } from 'react-hook-form';
|
||||
import {
|
||||
authLoginSchema,
|
||||
TLoginRequest,
|
||||
usePostLogin,
|
||||
} from '@imphnen-frontend-service/service';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { useSession } from '@imphnen-frontend-service/utils';
|
||||
import { useAuthStore } from '@imphnen-frontend-service/utils';
|
||||
import { toast } from 'sonner';
|
||||
import { useNavigate } from 'react-router';
|
||||
import { useVerifyEmail } from './use-verify-email';
|
||||
|
||||
export const useLogin = () => {
|
||||
const form = useForm<TLoginRequest>({
|
||||
@@ -12,12 +16,63 @@ export const useLogin = () => {
|
||||
mode: 'all',
|
||||
});
|
||||
|
||||
const { signIn } = useSession();
|
||||
const navigate = useNavigate();
|
||||
const { mutate, isPending } = usePostLogin();
|
||||
const { setLoading, setSession, clearSession } = useAuthStore();
|
||||
|
||||
const onSubmit = form.handleSubmit((data) => signIn(data));
|
||||
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,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -6,9 +6,20 @@ import {
|
||||
} from '@imphnen-frontend-service/service';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { toast } from 'sonner';
|
||||
import { useVerifyEmail } from './use-verify-email';
|
||||
|
||||
export const useRegister = () => {
|
||||
const postRegister = usePostRegister();
|
||||
const {
|
||||
verifyForm,
|
||||
onVerifySubmit,
|
||||
showVerifyModal,
|
||||
openVerifyModal,
|
||||
closeVerifyModal,
|
||||
isVerifying,
|
||||
emailToVerify,
|
||||
} = useVerifyEmail();
|
||||
|
||||
const form = useForm<TRegisterRequest>({
|
||||
resolver: zodResolver(authRegisterSchema),
|
||||
mode: 'all',
|
||||
@@ -23,13 +34,23 @@ export const useRegister = () => {
|
||||
|
||||
const onSubmit = form.handleSubmit((data) => {
|
||||
postRegister.mutate(data, {
|
||||
onSuccess: () => toast.success('Registrasi sukses'),
|
||||
onError: (error) => toast.error(error.message),
|
||||
onSuccess: () => {
|
||||
toast.success('Registrasi sukses. Silakan verifikasi email Anda.');
|
||||
openVerifyModal(data.email);
|
||||
},
|
||||
onError: (error) => toast.error(error.message || 'Registrasi gagal'),
|
||||
});
|
||||
});
|
||||
|
||||
return {
|
||||
form,
|
||||
verifyForm,
|
||||
onSubmit,
|
||||
onVerifySubmit,
|
||||
showVerifyModal,
|
||||
closeVerifyModal,
|
||||
isVerifying,
|
||||
isRegistering: postRegister.isPending,
|
||||
registeredEmail: emailToVerify,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
import { useForm } from 'react-hook-form';
|
||||
import {
|
||||
TVerifyEmailRequest,
|
||||
usePostVerifyEmail,
|
||||
} from '@imphnen-frontend-service/service';
|
||||
import { toast } from 'sonner';
|
||||
import { useState } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
|
||||
export const useVerifyEmail = () => {
|
||||
const [showVerifyModal, setShowVerifyModal] = useState(false);
|
||||
const [emailToVerify, setEmailToVerify] = useState('');
|
||||
const navigate = useNavigate();
|
||||
|
||||
const postVerifyEmail = usePostVerifyEmail();
|
||||
|
||||
const verifyForm = useForm<{ otp: string }>({
|
||||
mode: 'all',
|
||||
defaultValues: {
|
||||
otp: '',
|
||||
},
|
||||
});
|
||||
|
||||
const onVerifySubmit = verifyForm.handleSubmit((data) => {
|
||||
const otpNumber = data.otp;
|
||||
|
||||
const verifyData: TVerifyEmailRequest = {
|
||||
email: emailToVerify,
|
||||
otp: otpNumber,
|
||||
};
|
||||
|
||||
postVerifyEmail.mutate(verifyData, {
|
||||
onSuccess: () => {
|
||||
toast.success('Verifikasi email sukses');
|
||||
|
||||
navigate(0);
|
||||
},
|
||||
onError: (error) =>
|
||||
toast.error(error.message || 'Verifikasi email gagal'),
|
||||
});
|
||||
});
|
||||
|
||||
const openVerifyModal = (email: string) => {
|
||||
setEmailToVerify(email);
|
||||
setShowVerifyModal(true);
|
||||
};
|
||||
|
||||
const closeVerifyModal = () => {
|
||||
setShowVerifyModal(false);
|
||||
verifyForm.reset();
|
||||
};
|
||||
|
||||
return {
|
||||
verifyForm,
|
||||
onVerifySubmit,
|
||||
showVerifyModal,
|
||||
openVerifyModal,
|
||||
closeVerifyModal,
|
||||
isVerifying: postVerifyEmail.isPending,
|
||||
emailToVerify,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user