update: route
This commit is contained in:
@@ -1,158 +1,185 @@
|
||||
import { Flex, Input, Form, Button, Card, Space, Image } from 'antd'
|
||||
import React from 'react'
|
||||
// import handleSignIn from '../../Utils/Auth/SignIn';
|
||||
import React, { useState } from 'react';
|
||||
import { Flex, Input, Form, Button, Card, Space, Image, Row, Col, Typography } from 'antd';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import sypiu_ggcp from 'assets/sypiu_ggcp.jpg';
|
||||
import {useNavigate} from "react-router-dom";
|
||||
import logo from 'assets/freepik/LOGOPIU.png';
|
||||
import { register } from '../../api/auth';
|
||||
import { NotifOk, NotifAlert } from '../../components/Global/ToastNotif';
|
||||
|
||||
const SignUp = () => {
|
||||
|
||||
const [captchaSvg, setCaptchaSvg] = React.useState('');
|
||||
const [userInput, setUserInput] = React.useState('');
|
||||
const [message, setMessage] = React.useState('');
|
||||
|
||||
const [loading, setLoading] = useState(false);
|
||||
const navigate = useNavigate();
|
||||
// let url = `${import.meta.env.VITE_API_SERVER}/users`;
|
||||
|
||||
React.useEffect(() => {
|
||||
fetchCaptcha();
|
||||
}, []);
|
||||
const moveToSignin = () => {
|
||||
navigate('/signin');
|
||||
};
|
||||
|
||||
// Fetch the CAPTCHA SVG from the backend
|
||||
const fetchCaptcha = async () => {
|
||||
const handleSignUp = async (values) => {
|
||||
const { fullname, username, email, phone, password, confirmPassword } = values;
|
||||
|
||||
if (password !== confirmPassword) {
|
||||
NotifAlert({
|
||||
icon: 'error',
|
||||
title: 'Password Tidak Sama',
|
||||
message: 'Password dan confirm password harus sama',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const phoneRegex = /^(?:\+62|62|0)8[1-9][0-9]{6,11}$/;
|
||||
if (!phoneRegex.test(phone)) {
|
||||
NotifAlert({
|
||||
icon: 'error',
|
||||
title: 'Format Telepon Salah',
|
||||
message: 'Nomor telepon tidak valid (harus nomor Indonesia)',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const passwordErrors = [];
|
||||
if (password.length < 8) passwordErrors.push('Minimal 8 karakter');
|
||||
if (!/[A-Z]/.test(password)) passwordErrors.push('Harus ada huruf kapital');
|
||||
if (!/[0-9]/.test(password)) passwordErrors.push('Harus ada angka');
|
||||
if (!/[!@#$%^&*(),.?":{}|<>]/.test(password)) passwordErrors.push('Harus ada karakter spesial');
|
||||
if (passwordErrors.length) {
|
||||
NotifAlert({
|
||||
icon: 'error',
|
||||
title: 'Password Tidak Valid',
|
||||
message: passwordErrors.join(', '),
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
setLoading(true);
|
||||
try {
|
||||
const response = await fetch('http://localhost:9528/generate-captcha');
|
||||
const data = await response.text();
|
||||
setCaptchaSvg(data);
|
||||
} catch (error) {
|
||||
console.error('Error fetching CAPTCHA:', error);
|
||||
const res = await register({ fullname, username, email, phone, password });
|
||||
// const user = res?.data?.user;
|
||||
// // const tokens = res?.data?.tokens;
|
||||
|
||||
|
||||
NotifOk({
|
||||
icon: 'success',
|
||||
title: 'Registrasi Berhasil',
|
||||
message: res?.data?.message || 'Berhasil menambahkan user.',
|
||||
});
|
||||
|
||||
|
||||
// if (user) {
|
||||
// // localStorage.setItem('token', tokens.accessToken);
|
||||
// // localStorage.setItem('refreshToken', tokens.refreshToken);
|
||||
// // localStorage.setItem('user', JSON.stringify(user));
|
||||
|
||||
// NotifOk({
|
||||
// icon: 'success',
|
||||
// title: 'Registrasi Berhasil',
|
||||
// message: res?.data?.message || 'Selamat datang! Anda akan diarahkan ke dashboard.',
|
||||
// });
|
||||
|
||||
// navigate('/signin');
|
||||
// } else {
|
||||
// NotifAlert({
|
||||
// icon: 'error',
|
||||
// title: 'Registrasi Gagal',
|
||||
// message: res?.data?.message || 'Terjadi kesalahan',
|
||||
// });
|
||||
// }
|
||||
} catch (err) {
|
||||
console.error('Register error:', err);
|
||||
NotifAlert({
|
||||
icon: 'error',
|
||||
title: 'Registrasi Gagal',
|
||||
message: err?.response?.data?.message || err.message || 'Terjadi kesalahan',
|
||||
});
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleOnSubmt = async (e) => {
|
||||
// console.log('Received values of form: ', e);
|
||||
// await handleSignIn(e);
|
||||
e.preventDefault();
|
||||
|
||||
try {
|
||||
const response = await fetch('http://localhost:5000/verify-captcha', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ userInput }),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
if (data.success) {
|
||||
setMessage('CAPTCHA verified successfully!');
|
||||
} else {
|
||||
setMessage('CAPTCHA verification failed. Try again.');
|
||||
fetchCaptcha(); // Refresh CAPTCHA on failure
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error verifying CAPTCHA:', error);
|
||||
setMessage('An error occurred. Please try again.');
|
||||
}
|
||||
|
||||
setUserInput(''); // Clear the input field
|
||||
}
|
||||
|
||||
const moveToSignin = (e) => {
|
||||
// e.preventDefault();
|
||||
navigate("/signin")
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
|
||||
<Flex
|
||||
align='center'
|
||||
justify='center'
|
||||
// vertical
|
||||
style={{
|
||||
height: '100vh',
|
||||
// marginTop: '10vh',
|
||||
// backgroundImage: `url('https://via.placeholder.com/300')`,
|
||||
backgroundImage: `url(${sypiu_ggcp})`,
|
||||
backgroundSize: 'cover',
|
||||
backgroundPosition: 'center',
|
||||
<Flex
|
||||
align="center"
|
||||
justify="center"
|
||||
style={{
|
||||
minHeight: '100vh',
|
||||
backgroundImage: `url(${sypiu_ggcp})`,
|
||||
backgroundSize: 'cover',
|
||||
backgroundPosition: 'center',
|
||||
padding: '20px',
|
||||
}}
|
||||
>
|
||||
<Card
|
||||
style={{
|
||||
width: '100%',
|
||||
maxWidth: 450,
|
||||
borderRadius: '12px',
|
||||
boxShadow: '0 8px 16px rgba(0, 0, 0, 0.1)',
|
||||
padding: '10px',
|
||||
textAlign: 'center',
|
||||
}}
|
||||
>
|
||||
<Card
|
||||
style={{
|
||||
boxShadow: '0px 4px 8px rgba(0, 0, 0, 0.1)',
|
||||
}}
|
||||
{/* Logo di dalam Card */}
|
||||
<Image src={logo} height={120} width={180} preview={false} alt="logo"/>
|
||||
|
||||
<h2 style={{ marginBottom: '20px', color: '#1a3c34' }}>Registration</h2>
|
||||
|
||||
<Form onFinish={handleSignUp} layout="vertical">
|
||||
<Row gutter={16}>
|
||||
<Col span={12}>
|
||||
<Form.Item label="Full Name" name="fullname" rules={[{ required: true }]}>
|
||||
<Input placeholder="Full Name" size="large" />
|
||||
</Form.Item>
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
<Form.Item label="Username" name="username" rules={[{ required: true }]}>
|
||||
<Input placeholder="Username" size="large" />
|
||||
</Form.Item>
|
||||
</Col>
|
||||
</Row>
|
||||
|
||||
<Row gutter={16}>
|
||||
<Col span={12}>
|
||||
<Form.Item
|
||||
label="Email"
|
||||
name="email"
|
||||
rules={[{ required: true, type: 'email', message: 'Please input a valid email!' }]}
|
||||
>
|
||||
<Input placeholder="Email" size="large" />
|
||||
</Form.Item>
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
<Form.Item label="Phone" name="phone" rules={[{ required: true }]}>
|
||||
<Input placeholder="Phone" size="large" />
|
||||
</Form.Item>
|
||||
</Col>
|
||||
</Row>
|
||||
|
||||
<Form.Item label="Password" name="password" rules={[{ required: true }]}>
|
||||
<Input.Password placeholder="Password" size="large" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="Confirm Password" name="confirmPassword" rules={[{ required: true }]}>
|
||||
<Input.Password placeholder="Confirm Password" size="large" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item>
|
||||
<Space direction="vertical" style={{ width: '100%' }}>
|
||||
<Button type="primary" htmlType="submit" loading={loading} style={{ width: '100%' }} >
|
||||
Sign Up
|
||||
</Button>
|
||||
</Space>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
|
||||
<Button
|
||||
type="default"
|
||||
style={{ width: '100%', marginTop: '10px' }}
|
||||
onClick={moveToSignin}
|
||||
>
|
||||
<Flex align='center' justify='center'>
|
||||
<Image src='/vite.svg' height={150} width={150} preview={false} alt='signin' />
|
||||
</Flex>
|
||||
<br />
|
||||
<Form
|
||||
onFinish={handleOnSubmt}
|
||||
layout='vertical'
|
||||
style={{ width: '250px' }}
|
||||
>
|
||||
Sign In
|
||||
</Button>
|
||||
</Card>
|
||||
</Flex>
|
||||
);
|
||||
};
|
||||
|
||||
<Form.Item label="Email" name="email"
|
||||
rules={[
|
||||
{
|
||||
required: true,
|
||||
type: 'email'
|
||||
},
|
||||
]}
|
||||
>
|
||||
<Input placeholder='email' size='large' />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item label="Password" name="password"
|
||||
rules={[
|
||||
{
|
||||
required: true,
|
||||
message: 'Please input your password!'
|
||||
},
|
||||
]}
|
||||
>
|
||||
<Input.Password placeholder='password' size='large' />
|
||||
</Form.Item>
|
||||
<div dangerouslySetInnerHTML={{ __html: captchaSvg }} />
|
||||
|
||||
<Form.Item label="Captcha" name="captcha"
|
||||
rules={[
|
||||
{
|
||||
required: true,
|
||||
type: 'text'
|
||||
},
|
||||
]}
|
||||
>
|
||||
<Input placeholder='Enter CAPTCHA text' size='large' />
|
||||
</Form.Item>
|
||||
|
||||
{/* <input
|
||||
type="text"
|
||||
placeholder="Enter CAPTCHA text"
|
||||
value={userInput}
|
||||
onChange={(e) => setUserInput(e.target.value)}
|
||||
/> */}
|
||||
|
||||
<Form.Item>
|
||||
<Space direction='vertical' style={{ width: '100%' }}>
|
||||
<Button type="primary" htmlType="submit" style={{ width: '100%' }}>
|
||||
Registrasi
|
||||
</Button>
|
||||
</Space>
|
||||
</Form.Item>
|
||||
|
||||
</Form>
|
||||
<Button
|
||||
type="primary"
|
||||
htmlType="submit"
|
||||
style={{ width: '100%' }}
|
||||
onClick={() => moveToSignin()}
|
||||
>
|
||||
Sign In
|
||||
</Button>
|
||||
</Card>
|
||||
</Flex>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default SignUp
|
||||
export default SignUp;
|
||||
|
||||
Reference in New Issue
Block a user