import { Flex, Input, Form, Button, Card, Space, Image } from 'antd'; import React from 'react'; import handleSignIn from '../../Utils/Auth/SignIn'; import sypiu_ggcp from 'assets/sypiu_ggcp.jpg'; import logo from 'assets/freepik/LOGOPIU.png'; import { useNavigate } from 'react-router-dom'; import { NotifAlert } from '../../components/Global/ToastNotif'; import { decryptData } from '../../components/Global/Formatter'; const SignIn = () => { const [captchaSvg, setCaptchaSvg] = React.useState(''); const [userInput, setUserInput] = React.useState(''); const [message, setMessage] = React.useState(''); const [captchaText, setcaptchaText] = React.useState(''); const navigate = useNavigate(); // let url = `${import.meta.env.VITE_API_SERVER}/users`; React.useEffect(() => { fetchCaptcha(); }, []); // Fetch the CAPTCHA SVG from the backend const fetchCaptcha = async () => { try { // let url = `${import.meta.env.VITE_API_SERVER}/operation` // const response = await fetch('http://localhost:9528/generate-captcha'); const response = await fetch( `${import.meta.env.VITE_API_SERVER}/auth/generate-captcha`, { credentials: 'include', // Wajib untuk mengirim cookie } ); // Ambil header const captchaToken = response.headers.get('X-Captcha-Token'); // console.log('Captcha Token:', decryptData(captchaToken)); setcaptchaText(decryptData(captchaToken)); const data = await response.text(); setCaptchaSvg(data); } catch (error) { console.error('Error fetching CAPTCHA:', error); } }; const handleOnSubmit = async (e) => { // console.log('Received values of form: ', e); // e.preventDefault(); try { // const response = await fetch(`${import.meta.env.VITE_API_SERVER}/auth/verify-captcha`, { // method: 'POST', // headers: { 'Content-Type': 'application/json' }, // credentials: 'include', // WAJIB: Agar cookie CAPTCHA dikirim // body: JSON.stringify({ captcha: userInput }), // }); // const data = await response.json(); // console.log(data); const data = { success: captchaText === userInput ? true : false, }; if (data.success) { setMessage('CAPTCHA verified successfully!'); await handleSignIn(e); } else { setMessage('CAPTCHA verification failed. Try again.'); fetchCaptcha(); // Refresh CAPTCHA on failure NotifAlert({ icon: 'error', title: 'Gagal', message: data.message || 'CAPTCHA verification failed. Try again.', }); } } catch (error) { console.error('Error verifying CAPTCHA:', error); setMessage('An error occurred. Please try again.'); } setUserInput(''); // Clear the input field }; const moveToRegistration = (e) => { // e.preventDefault(); // navigate("/signup") navigate('/registration'); }; return ( <> signin
{/* {message} */} setUserInput(e.target.value)} /> ); }; export default SignIn;