import React, { useState } from 'react'; import { Container, Paper, Box, Typography, TextField, Button, Link, Alert, InputAdornment, IconButton, } from '@mui/material'; import { Visibility, VisibilityOff, Email, Lock, Person, ShoppingCart, Google, } from '@mui/icons-material'; import { useForm, Controller } from 'react-hook-form'; import { yupResolver } from '@hookform/resolvers/yup'; import * as yup from 'yup'; import { useNavigate } from 'react-router-dom'; import { useAuth } from '../../contexts/AuthContext'; import { RegisterForm } from '../../types'; import toast from 'react-hot-toast'; const schema = yup.object({ firstName: yup .string() .required('Ad gerekli') .min(2, 'Ad en az 2 karakter olmalı'), lastName: yup .string() .required('Soyad gerekli') .min(2, 'Soyad en az 2 karakter olmalı'), email: yup .string() .email('Lütfen geçerli bir e-posta adresi girin') .required('E-posta gerekli'), password: yup .string() .min(6, 'Şifre en az 6 karakter olmalı') .matches( /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)/, 'Şifre en az bir büyük harf, bir küçük harf ve bir rakam içermeli' ) .required('Şifre gerekli'), confirmPassword: yup .string() .oneOf([yup.ref('password')], 'Şifreler eşleşmeli') .required('Lütfen şifrenizi onaylayın'), }); const RegisterPage: React.FC = () => { const navigate = useNavigate(); const { register, isLoading } = useAuth(); const [showPassword, setShowPassword] = useState(false); const [showConfirmPassword, setShowConfirmPassword] = useState(false); const [error, setError] = useState(''); const { control, handleSubmit, formState: { errors }, } = useForm({ resolver: yupResolver(schema), defaultValues: { firstName: '', lastName: '', email: '', password: '', confirmPassword: '', }, }); const onSubmit = async (data: RegisterForm) => { try { setError(''); await register({ firstName: data.firstName, lastName: data.lastName, email: data.email, password: data.password, confirmPassword: data.confirmPassword, }); toast.success('Hesap başarıyla oluşturuldu!'); navigate('/dashboard'); } catch (err: any) { const errorMessage = err.response?.data?.message || 'Kayıt başarısız. Lütfen tekrar deneyin.'; setError(errorMessage); toast.error(errorMessage); } }; const handleClickShowPassword = () => { setShowPassword(!showPassword); }; const handleClickShowConfirmPassword = () => { setShowConfirmPassword(!showConfirmPassword); }; const handleGoogleLogin = () => { // Backend'deki Google OAuth endpoint'ine yönlendir const apiUrl = process.env.REACT_APP_API_URL || 'http://localhost:7001/api'; window.location.href = `${apiUrl}/auth/google`; }; return ( {/* Logo and Title */} HMarket Hesap Oluştur {error && ( {error} )} ( ), }} /> )} /> ( ), }} /> )} /> ( ), }} /> )} /> ( ), endAdornment: ( {showPassword ? : } ), }} /> )} /> ( ), endAdornment: ( {showConfirmPassword ? : } ), }} /> )} /> Zaten hesabınız var mı?{' '} navigate('/login')} sx={{ textDecoration: 'none', fontWeight: 'bold' }} > Giriş Yap {/* Terms */} By creating an account, you agree to our{' '} Terms of Service {' '} and{' '} Privacy Policy ); }; export default RegisterPage;