update to python fastpi

This commit is contained in:
Iliyan Angelov
2025-11-16 15:59:05 +02:00
parent 93d4c1df80
commit 98ccd5b6ff
4464 changed files with 773233 additions and 13740 deletions

View File

@@ -0,0 +1,328 @@
import React, { useState } from 'react';
import { useForm } from 'react-hook-form';
import { yupResolver } from '@hookform/resolvers/yup';
import { Link } from 'react-router-dom';
import {
Mail,
ArrowLeft,
Send,
Loader2,
CheckCircle,
Hotel,
} from 'lucide-react';
import useAuthStore from '../../store/useAuthStore';
import {
forgotPasswordSchema,
ForgotPasswordFormData,
} from '../../utils/validationSchemas';
const ForgotPasswordPage: React.FC = () => {
const { forgotPassword, isLoading, error, clearError } =
useAuthStore();
const [isSuccess, setIsSuccess] = useState(false);
const [submittedEmail, setSubmittedEmail] = useState('');
// React Hook Form setup
const {
register,
handleSubmit,
formState: { errors },
} = useForm<ForgotPasswordFormData>({
resolver: yupResolver(forgotPasswordSchema),
defaultValues: {
email: '',
},
});
// Handle form submission
const onSubmit = async (data: ForgotPasswordFormData) => {
try {
clearError();
setSubmittedEmail(data.email);
await forgotPassword({ email: data.email });
// Show success state
setIsSuccess(true);
} catch (error) {
// Error has been handled in store
console.error('Forgot password error:', error);
}
};
return (
<div
className="min-h-screen bg-gradient-to-br
from-blue-50 to-indigo-100 flex items-center
justify-center py-12 px-4 sm:px-6 lg:px-8"
>
<div className="max-w-md w-full space-y-8">
{/* Header */}
<div className="text-center">
<div className="flex justify-center mb-4">
<div className="p-3 bg-blue-600 rounded-full">
<Hotel className="w-12 h-12 text-white" />
</div>
</div>
<h2 className="text-3xl font-bold text-gray-900">
Forgot Password?
</h2>
<p className="mt-2 text-sm text-gray-600">
Enter your email to receive a password reset link
</p>
</div>
{/* Form Container */}
<div className="bg-white rounded-lg shadow-xl p-8">
{isSuccess ? (
// Success State
<div className="text-center space-y-6">
<div className="flex justify-center">
<div
className="w-16 h-16 bg-green-100
rounded-full flex items-center
justify-center"
>
<CheckCircle
className="w-10 h-10 text-green-600"
/>
</div>
</div>
<div className="space-y-2">
<h3
className="text-xl font-semibold
text-gray-900"
>
Email Sent!
</h3>
<p className="text-sm text-gray-600">
We have sent a password reset link to
</p>
<p className="text-sm font-medium text-blue-600">
{submittedEmail}
</p>
</div>
<div
className="bg-blue-50 border border-blue-200
rounded-lg p-4 text-left"
>
<p className="text-sm text-gray-700">
<strong>Note:</strong>
</p>
<ul
className="mt-2 space-y-1 text-sm
text-gray-600 list-disc list-inside"
>
<li>Link is valid for 1 hour</li>
<li>Check your Spam/Junk folder</li>
<li>
If you don't receive the email, please try again
</li>
</ul>
</div>
<div className="space-y-3">
<button
onClick={() => {
setIsSuccess(false);
clearError();
}}
className="w-full flex items-center
justify-center py-3 px-4 border
border-gray-300 rounded-lg
text-sm font-medium text-gray-700
bg-white hover:bg-gray-50
focus:outline-none focus:ring-2
focus:ring-offset-2
focus:ring-blue-500
transition-colors"
>
<Mail className="-ml-1 mr-2 h-5 w-5" />
Resend Email
</button>
<Link
to="/login"
className="w-full flex items-center
justify-center py-3 px-4 border
border-transparent rounded-lg
text-sm font-medium text-white
bg-blue-600 hover:bg-blue-700
focus:outline-none focus:ring-2
focus:ring-offset-2
focus:ring-blue-500
transition-colors"
>
<ArrowLeft
className="-ml-1 mr-2 h-5 w-5"
/>
Back to Login
</Link>
</div>
</div>
) : (
// Form State
<form
onSubmit={handleSubmit(onSubmit)}
className="space-y-6"
>
{/* Error Message */}
{error && (
<div
className="bg-red-50 border
border-red-200 text-red-700
px-4 py-3 rounded-lg text-sm"
>
{error}
</div>
)}
{/* Email Field */}
<div>
<label
htmlFor="email"
className="block text-sm font-medium
text-gray-700 mb-2"
>
Email
</label>
<div className="relative">
<div
className="absolute inset-y-0 left-0
pl-3 flex items-center
pointer-events-none"
>
<Mail
className="h-5 w-5 text-gray-400"
/>
</div>
<input
{...register('email')}
id="email"
type="email"
autoComplete="email"
autoFocus
className={`block w-full pl-10 pr-3
py-3 border rounded-lg
focus:outline-none focus:ring-2
transition-colors
${
errors.email
? 'border-red-300 ' +
'focus:ring-red-500'
: 'border-gray-300 ' +
'focus:ring-blue-500'
}`}
placeholder="email@example.com"
/>
</div>
{errors.email && (
<p className="mt-1 text-sm text-red-600">
{errors.email.message}
</p>
)}
</div>
{/* Submit Button */}
<button
type="submit"
disabled={isLoading}
className="w-full flex items-center
justify-center py-3 px-4 border
border-transparent rounded-lg
shadow-sm text-sm font-medium
text-white bg-blue-600
hover:bg-blue-700 focus:outline-none
focus:ring-2 focus:ring-offset-2
focus:ring-blue-500
disabled:opacity-50
disabled:cursor-not-allowed
transition-colors"
>
{isLoading ? (
<>
<Loader2
className="animate-spin -ml-1
mr-2 h-5 w-5"
/>
Processing...
</>
) : (
<>
<Send className="-ml-1 mr-2 h-5 w-5" />
Send Reset Link
</>
)}
</button>
{/* Back to Login Link */}
<div className="text-center">
<Link
to="/login"
className="inline-flex items-center
text-sm font-medium text-blue-600
hover:text-blue-500 transition-colors"
>
<ArrowLeft
className="mr-1 h-4 w-4"
/>
Back to Login
</Link>
</div>
</form>
)}
</div>
{/* Footer Info */}
{!isSuccess && (
<div className="text-center text-sm text-gray-500">
<p>
Don't have an account?{' '}
<Link
to="/register"
className="font-medium text-blue-600
hover:underline"
>
Register now
</Link>
</p>
</div>
)}
{/* Help Section */}
<div
className="bg-white rounded-lg shadow-sm
border border-gray-200 p-4"
>
<h3
className="text-sm font-semibold text-gray-900
mb-2"
>
Need Help?
</h3>
<p className="text-xs text-gray-600">
If you're having trouble resetting your password,
please contact our support team via email{' '}
<a
href="mailto:support@hotel.com"
className="text-blue-600 hover:underline"
>
support@hotel.com
</a>{' '}
or hotline{' '}
<a
href="tel:1900-xxxx"
className="text-blue-600 hover:underline"
>
1900-xxxx
</a>
</p>
</div>
</div>
</div>
);
};
export default ForgotPasswordPage;

View File

@@ -0,0 +1,287 @@
import React, { useState } from 'react';
import { useForm } from 'react-hook-form';
import { yupResolver } from '@hookform/resolvers/yup';
import { Link, useNavigate, useLocation } from 'react-router-dom';
import {
Eye,
EyeOff,
LogIn,
Loader2,
Mail,
Lock,
Hotel
} from 'lucide-react';
import useAuthStore from '../../store/useAuthStore';
import {
loginSchema,
LoginFormData
} from '../../utils/validationSchemas';
const LoginPage: React.FC = () => {
const navigate = useNavigate();
const location = useLocation();
const { login, isLoading, error, clearError } =
useAuthStore();
const [showPassword, setShowPassword] = useState(false);
// React Hook Form setup
const {
register,
handleSubmit,
formState: { errors },
} = useForm<LoginFormData>({
resolver: yupResolver(loginSchema),
defaultValues: {
email: '',
password: '',
rememberMe: false,
},
});
// Handle form submission
const onSubmit = async (data: LoginFormData) => {
try {
clearError();
await login({
email: data.email,
password: data.password,
rememberMe: data.rememberMe,
});
// Redirect to previous page or dashboard
const from = location.state?.from?.pathname ||
'/dashboard';
navigate(from, { replace: true });
} catch (error) {
// Error has been handled in store
console.error('Login error:', error);
}
};
return (
<div className="min-h-screen bg-gradient-to-br
from-blue-50 to-indigo-100 flex items-center
justify-center py-12 px-4 sm:px-6 lg:px-8"
>
<div className="max-w-md w-full space-y-8">
{/* Header */}
<div className="text-center">
<div className="flex justify-center mb-4">
<div className="p-3 bg-blue-600 rounded-full">
<Hotel className="w-12 h-12 text-white" />
</div>
</div>
<h2 className="text-3xl font-bold text-gray-900">
Login
</h2>
<p className="mt-2 text-sm text-gray-600">
Welcome back to Hotel Booking
</p>
</div>
{/* Login Form */}
<div className="bg-white rounded-lg shadow-xl p-8">
<form onSubmit={handleSubmit(onSubmit)}
className="space-y-6"
>
{/* Error Message */}
{error && (
<div className="bg-red-50 border border-red-200
text-red-700 px-4 py-3 rounded-lg
text-sm"
>
{error}
</div>
)}
{/* Email Field */}
<div>
<label
htmlFor="email"
className="block text-sm font-medium
text-gray-700 mb-2"
>
Email
</label>
<div className="relative">
<div className="absolute inset-y-0 left-0
pl-3 flex items-center pointer-events-none"
>
<Mail className="h-5 w-5 text-gray-400" />
</div>
<input
{...register('email')}
id="email"
type="email"
autoComplete="email"
className={`block w-full pl-10 pr-3 py-3
border rounded-lg focus:outline-none
focus:ring-2 transition-colors
${errors.email
? 'border-red-300 focus:ring-red-500'
: 'border-gray-300 focus:ring-blue-500'
}`}
placeholder="email@example.com"
/>
</div>
{errors.email && (
<p className="mt-1 text-sm text-red-600">
{errors.email.message}
</p>
)}
</div>
{/* Password Field */}
<div>
<label
htmlFor="password"
className="block text-sm font-medium
text-gray-700 mb-2"
>
Password
</label>
<div className="relative">
<div className="absolute inset-y-0 left-0
pl-3 flex items-center pointer-events-none"
>
<Lock className="h-5 w-5 text-gray-400" />
</div>
<input
{...register('password')}
id="password"
type={showPassword ? 'text' : 'password'}
autoComplete="current-password"
className={`block w-full pl-10 pr-10 py-3
border rounded-lg focus:outline-none
focus:ring-2 transition-colors
${errors.password
? 'border-red-300 focus:ring-red-500'
: 'border-gray-300 focus:ring-blue-500'
}`}
placeholder="••••••••"
/>
<button
type="button"
onClick={() => setShowPassword(!showPassword)}
className="absolute inset-y-0 right-0
pr-3 flex items-center"
>
{showPassword ? (
<EyeOff className="h-5 w-5
text-gray-400 hover:text-gray-600"
/>
) : (
<Eye className="h-5 w-5 text-gray-400
hover:text-gray-600"
/>
)}
</button>
</div>
{errors.password && (
<p className="mt-1 text-sm text-red-600">
{errors.password.message}
</p>
)}
</div>
{/* Remember Me & Forgot Password */}
<div className="flex items-center justify-between">
<div className="flex items-center">
<input
{...register('rememberMe')}
id="rememberMe"
type="checkbox"
className="h-4 w-4 text-blue-600
focus:ring-blue-500 border-gray-300
rounded cursor-pointer"
/>
<label
htmlFor="rememberMe"
className="ml-2 block text-sm
text-gray-700 cursor-pointer"
>
Remember me
</label>
</div>
<Link
to="/forgot-password"
className="text-sm font-medium
text-blue-600 hover:text-blue-500
transition-colors"
>
Forgot password?
</Link>
</div>
{/* Submit Button */}
<button
type="submit"
disabled={isLoading}
className="w-full flex items-center
justify-center py-3 px-4 border
border-transparent rounded-lg shadow-sm
text-sm font-medium text-white
bg-blue-600 hover:bg-blue-700
focus:outline-none focus:ring-2
focus:ring-offset-2 focus:ring-blue-500
disabled:opacity-50 disabled:cursor-not-allowed
transition-colors"
>
{isLoading ? (
<>
<Loader2 className="animate-spin -ml-1
mr-2 h-5 w-5"
/>
Processing...
</>
) : (
<>
<LogIn className="-ml-1 mr-2 h-5 w-5" />
Login
</>
)}
</button>
</form>
{/* Register Link */}
<div className="mt-6 text-center">
<p className="text-sm text-gray-600">
Don't have an account?{' '}
<Link
to="/register"
className="font-medium text-blue-600
hover:text-blue-500 transition-colors"
>
Register now
</Link>
</p>
</div>
</div>
{/* Footer Info */}
<div className="text-center text-sm text-gray-500">
<p>
By logging in, you agree to our{' '}
<Link
to="/terms"
className="text-blue-600 hover:underline"
>
Terms of Service
</Link>{' '}
and{' '}
<Link
to="/privacy"
className="text-blue-600 hover:underline"
>
Privacy Policy
</Link>
</p>
</div>
</div>
</div>
);
};
export default LoginPage;

View File

@@ -0,0 +1,517 @@
import React, { useState } from 'react';
import { useForm } from 'react-hook-form';
import { yupResolver } from '@hookform/resolvers/yup';
import { Link, useNavigate } from 'react-router-dom';
import {
Eye,
EyeOff,
UserPlus,
Loader2,
Mail,
Lock,
User,
Phone,
Hotel,
CheckCircle2,
XCircle,
} from 'lucide-react';
import useAuthStore from '../../store/useAuthStore';
import {
registerSchema,
RegisterFormData,
} from '../../utils/validationSchemas';
const RegisterPage: React.FC = () => {
const navigate = useNavigate();
const { register: registerUser, isLoading, error, clearError } =
useAuthStore();
const [showPassword, setShowPassword] = useState(false);
const [showConfirmPassword, setShowConfirmPassword] =
useState(false);
// React Hook Form setup
const {
register,
handleSubmit,
watch,
formState: { errors },
} = useForm<RegisterFormData>({
resolver: yupResolver(registerSchema),
defaultValues: {
name: '',
email: '',
password: '',
confirmPassword: '',
phone: '',
},
});
// Watch password to display password strength
const password = watch('password');
// Password strength checker
const getPasswordStrength = (pwd: string) => {
if (!pwd) return { strength: 0, label: '', color: '' };
let strength = 0;
if (pwd.length >= 8) strength++;
if (/[a-z]/.test(pwd)) strength++;
if (/[A-Z]/.test(pwd)) strength++;
if (/\d/.test(pwd)) strength++;
if (/[@$!%*?&]/.test(pwd)) strength++;
const labels = [
{ label: 'Very Weak', color: 'bg-red-500' },
{ label: 'Weak', color: 'bg-orange-500' },
{ label: 'Medium', color: 'bg-yellow-500' },
{ label: 'Strong', color: 'bg-blue-500' },
{ label: 'Very Strong', color: 'bg-green-500' },
];
return { strength, ...labels[strength] };
};
const passwordStrength = getPasswordStrength(password || '');
// Handle form submission
const onSubmit = async (data: RegisterFormData) => {
try {
clearError();
await registerUser({
name: data.name,
email: data.email,
password: data.password,
phone: data.phone,
});
// Redirect to login page
navigate('/login', { replace: true });
} catch (error) {
// Error has been handled in store
console.error('Register error:', error);
}
};
return (
<div
className="min-h-screen bg-gradient-to-br
from-purple-50 to-pink-100 flex items-center
justify-center py-12 px-4 sm:px-6 lg:px-8"
>
<div className="max-w-md w-full space-y-8">
{/* Header */}
<div className="text-center">
<div className="flex justify-center mb-4">
<div className="p-3 bg-purple-600 rounded-full">
<Hotel className="w-12 h-12 text-white" />
</div>
</div>
<h2 className="text-3xl font-bold text-gray-900">
Create Account
</h2>
<p className="mt-2 text-sm text-gray-600">
Create a new account to book hotel rooms
</p>
</div>
{/* Register Form */}
<div className="bg-white rounded-lg shadow-xl p-8">
<form
onSubmit={handleSubmit(onSubmit)}
className="space-y-5"
>
{/* Error Message */}
{error && (
<div
className="bg-red-50 border border-red-200
text-red-700 px-4 py-3 rounded-lg
text-sm"
>
{error}
</div>
)}
{/* Name Field */}
<div>
<label
htmlFor="name"
className="block text-sm font-medium
text-gray-700 mb-2"
>
Full Name
</label>
<div className="relative">
<div
className="absolute inset-y-0 left-0
pl-3 flex items-center
pointer-events-none"
>
<User className="h-5 w-5 text-gray-400" />
</div>
<input
{...register('name')}
id="name"
type="text"
autoComplete="name"
className={`block w-full pl-10 pr-3 py-3
border rounded-lg focus:outline-none
focus:ring-2 transition-colors
${
errors.name
? 'border-red-300 focus:ring-red-500'
: 'border-gray-300 ' +
'focus:ring-purple-500'
}`}
placeholder="John Doe"
/>
</div>
{errors.name && (
<p className="mt-1 text-sm text-red-600">
{errors.name.message}
</p>
)}
</div>
{/* Email Field */}
<div>
<label
htmlFor="email"
className="block text-sm font-medium
text-gray-700 mb-2"
>
Email
</label>
<div className="relative">
<div
className="absolute inset-y-0 left-0
pl-3 flex items-center
pointer-events-none"
>
<Mail className="h-5 w-5 text-gray-400" />
</div>
<input
{...register('email')}
id="email"
type="email"
autoComplete="email"
className={`block w-full pl-10 pr-3 py-3
border rounded-lg focus:outline-none
focus:ring-2 transition-colors
${
errors.email
? 'border-red-300 focus:ring-red-500'
: 'border-gray-300 ' +
'focus:ring-purple-500'
}`}
placeholder="email@example.com"
/>
</div>
{errors.email && (
<p className="mt-1 text-sm text-red-600">
{errors.email.message}
</p>
)}
</div>
{/* Phone Field */}
<div>
<label
htmlFor="phone"
className="block text-sm font-medium
text-gray-700 mb-2"
>
Phone Number (Optional)
</label>
<div className="relative">
<div
className="absolute inset-y-0 left-0
pl-3 flex items-center
pointer-events-none"
>
<Phone className="h-5 w-5 text-gray-400" />
</div>
<input
{...register('phone')}
id="phone"
type="tel"
autoComplete="tel"
className={`block w-full pl-10 pr-3 py-3
border rounded-lg focus:outline-none
focus:ring-2 transition-colors
${
errors.phone
? 'border-red-300 focus:ring-red-500'
: 'border-gray-300 ' +
'focus:ring-purple-500'
}`}
placeholder="0123456789"
/>
</div>
{errors.phone && (
<p className="mt-1 text-sm text-red-600">
{errors.phone.message}
</p>
)}
</div>
{/* Password Field */}
<div>
<label
htmlFor="password"
className="block text-sm font-medium
text-gray-700 mb-2"
>
Password
</label>
<div className="relative">
<div
className="absolute inset-y-0 left-0
pl-3 flex items-center
pointer-events-none"
>
<Lock className="h-5 w-5 text-gray-400" />
</div>
<input
{...register('password')}
id="password"
type={showPassword ? 'text' : 'password'}
autoComplete="new-password"
className={`block w-full pl-10 pr-10 py-3
border rounded-lg focus:outline-none
focus:ring-2 transition-colors
${
errors.password
? 'border-red-300 focus:ring-red-500'
: 'border-gray-300 ' +
'focus:ring-purple-500'
}`}
placeholder="••••••••"
/>
<button
type="button"
onClick={() => setShowPassword(!showPassword)}
className="absolute inset-y-0 right-0
pr-3 flex items-center"
>
{showPassword ? (
<EyeOff
className="h-5 w-5 text-gray-400
hover:text-gray-600"
/>
) : (
<Eye
className="h-5 w-5 text-gray-400
hover:text-gray-600"
/>
)}
</button>
</div>
{errors.password && (
<p className="mt-1 text-sm text-red-600">
{errors.password.message}
</p>
)}
{/* Password Strength Indicator */}
{password && password.length > 0 && (
<div className="mt-2">
<div className="flex items-center gap-2">
<div className="flex-1 h-2 bg-gray-200
rounded-full overflow-hidden"
>
<div
className={`h-full transition-all
duration-300 ${passwordStrength.color}`}
style={{
width: `${
(passwordStrength.strength / 5) * 100
}%`,
}}
/>
</div>
<span className="text-xs font-medium
text-gray-600"
>
{passwordStrength.label}
</span>
</div>
{/* Password Requirements */}
<div className="mt-2 space-y-1">
<PasswordRequirement
met={password.length >= 8}
text="At least 8 characters"
/>
<PasswordRequirement
met={/[a-z]/.test(password)}
text="Lowercase letter (a-z)"
/>
<PasswordRequirement
met={/[A-Z]/.test(password)}
text="Uppercase letter (A-Z)"
/>
<PasswordRequirement
met={/\d/.test(password)}
text="Number (0-9)"
/>
<PasswordRequirement
met={/[@$!%*?&]/.test(password)}
text="Special character (@$!%*?&)"
/>
</div>
</div>
)}
</div>
{/* Confirm Password Field */}
<div>
<label
htmlFor="confirmPassword"
className="block text-sm font-medium
text-gray-700 mb-2"
>
Confirm Password
</label>
<div className="relative">
<div
className="absolute inset-y-0 left-0
pl-3 flex items-center
pointer-events-none"
>
<Lock className="h-5 w-5 text-gray-400" />
</div>
<input
{...register('confirmPassword')}
id="confirmPassword"
type={
showConfirmPassword ? 'text' : 'password'
}
autoComplete="new-password"
className={`block w-full pl-10 pr-10 py-3
border rounded-lg focus:outline-none
focus:ring-2 transition-colors
${
errors.confirmPassword
? 'border-red-300 focus:ring-red-500'
: 'border-gray-300 ' +
'focus:ring-purple-500'
}`}
placeholder="••••••••"
/>
<button
type="button"
onClick={() =>
setShowConfirmPassword(!showConfirmPassword)
}
className="absolute inset-y-0 right-0
pr-3 flex items-center"
>
{showConfirmPassword ? (
<EyeOff
className="h-5 w-5 text-gray-400
hover:text-gray-600"
/>
) : (
<Eye
className="h-5 w-5 text-gray-400
hover:text-gray-600"
/>
)}
</button>
</div>
{errors.confirmPassword && (
<p className="mt-1 text-sm text-red-600">
{errors.confirmPassword.message}
</p>
)}
</div>
{/* Submit Button */}
<button
type="submit"
disabled={isLoading}
className="w-full flex items-center
justify-center py-3 px-4 border
border-transparent rounded-lg shadow-sm
text-sm font-medium text-white
bg-purple-600 hover:bg-purple-700
focus:outline-none focus:ring-2
focus:ring-offset-2 focus:ring-purple-500
disabled:opacity-50
disabled:cursor-not-allowed
transition-colors"
>
{isLoading ? (
<>
<Loader2 className="animate-spin -ml-1
mr-2 h-5 w-5"
/>
Processing...
</>
) : (
<>
<UserPlus className="-ml-1 mr-2 h-5 w-5" />
Register
</>
)}
</button>
</form>
{/* Login Link */}
<div className="mt-6 text-center">
<p className="text-sm text-gray-600">
Already have an account?{' '}
<Link
to="/login"
className="font-medium text-purple-600
hover:text-purple-500 transition-colors"
>
Login now
</Link>
</p>
</div>
</div>
{/* Footer Info */}
<div className="text-center text-sm text-gray-500">
<p>
By registering, you agree to our{' '}
<Link
to="/terms"
className="text-purple-600 hover:underline"
>
Terms of Service
</Link>{' '}
and{' '}
<Link
to="/privacy"
className="text-purple-600 hover:underline"
>
Privacy Policy
</Link>
</p>
</div>
</div>
</div>
);
};
// Helper component for password requirements
const PasswordRequirement: React.FC<{
met: boolean;
text: string;
}> = ({ met, text }) => (
<div className="flex items-center gap-2 text-xs">
{met ? (
<CheckCircle2 className="h-4 w-4 text-green-500" />
) : (
<XCircle className="h-4 w-4 text-gray-300" />
)}
<span className={met ? 'text-green-600' : 'text-gray-500'}>
{text}
</span>
</div>
);
export default RegisterPage;

View File

@@ -0,0 +1,531 @@
import React, { useState, useEffect } from 'react';
import { useForm } from 'react-hook-form';
import { yupResolver } from '@hookform/resolvers/yup';
import { useNavigate, useParams, Link } from 'react-router-dom';
import {
Eye,
EyeOff,
Lock,
Loader2,
CheckCircle2,
XCircle,
AlertCircle,
KeyRound,
Hotel,
} from 'lucide-react';
import useAuthStore from '../../store/useAuthStore';
import {
resetPasswordSchema,
ResetPasswordFormData,
} from '../../utils/validationSchemas';
const ResetPasswordPage: React.FC = () => {
const navigate = useNavigate();
const { token } = useParams<{ token: string }>();
const { resetPassword, isLoading, error, clearError } =
useAuthStore();
const [showPassword, setShowPassword] = useState(false);
const [showConfirmPassword, setShowConfirmPassword] =
useState(false);
const [isSuccess, setIsSuccess] = useState(false);
// React Hook Form setup
const {
register,
handleSubmit,
watch,
formState: { errors },
} = useForm<ResetPasswordFormData>({
resolver: yupResolver(resetPasswordSchema),
defaultValues: {
password: '',
confirmPassword: '',
},
});
// Watch password to display password strength
const password = watch('password');
// Check if token exists
useEffect(() => {
if (!token) {
navigate('/forgot-password', { replace: true });
}
}, [token, navigate]);
// Password strength checker
const getPasswordStrength = (pwd: string) => {
if (!pwd) return { strength: 0, label: '', color: '' };
let strength = 0;
if (pwd.length >= 8) strength++;
if (/[a-z]/.test(pwd)) strength++;
if (/[A-Z]/.test(pwd)) strength++;
if (/\d/.test(pwd)) strength++;
if (/[@$!%*?&]/.test(pwd)) strength++;
const labels = [
{ label: 'Very Weak', color: 'bg-red-500' },
{ label: 'Weak', color: 'bg-orange-500' },
{ label: 'Medium', color: 'bg-yellow-500' },
{ label: 'Strong', color: 'bg-blue-500' },
{ label: 'Very Strong', color: 'bg-green-500' },
];
return { strength, ...labels[strength] };
};
const passwordStrength = getPasswordStrength(password || '');
// Handle form submission
const onSubmit = async (data: ResetPasswordFormData) => {
if (!token) {
return;
}
try {
clearError();
await resetPassword({
token,
password: data.password,
confirmPassword: data.confirmPassword,
});
// Show success state
setIsSuccess(true);
// Redirect to login after 3 seconds
setTimeout(() => {
navigate('/login', { replace: true });
}, 3000);
} catch (error) {
// Error has been handled in store
console.error('Reset password error:', error);
}
};
// Invalid token error check
const isTokenError =
error?.includes('token') || error?.includes('expired');
// New password reuse error check
const isReuseError =
error?.toLowerCase().includes('must be different') ||
error?.toLowerCase().includes('different from old');
return (
<div
className="min-h-screen bg-gradient-to-br
from-indigo-50 to-purple-100 flex items-center
justify-center py-12 px-4 sm:px-6 lg:px-8"
>
<div className="max-w-md w-full space-y-8">
{/* Header */}
<div className="text-center">
<div className="flex justify-center mb-4">
<div className="p-3 bg-indigo-600 rounded-full">
<Hotel className="w-12 h-12 text-white" />
</div>
</div>
<h2 className="text-3xl font-bold text-gray-900">
{isSuccess ? 'Complete!' : 'Reset Password'}
</h2>
<p className="mt-2 text-sm text-gray-600">
{isSuccess
? 'Password has been reset successfully'
: 'Enter a new password for your account'}
</p>
</div>
{/* Form Container */}
<div className="bg-white rounded-lg shadow-xl p-8">
{isSuccess ? (
// Success State
<div className="text-center space-y-6">
<div className="flex justify-center">
<div
className="w-16 h-16 bg-green-100
rounded-full flex items-center
justify-center"
>
<CheckCircle2
className="w-10 h-10 text-green-600"
/>
</div>
</div>
<div className="space-y-2">
<h3
className="text-xl font-semibold
text-gray-900"
>
Password reset successful!
</h3>
<p className="text-sm text-gray-600">
Your password has been updated.
</p>
<p className="text-sm text-gray-600">
You can now login with your new password.
</p>
</div>
<div
className="bg-blue-50 border border-blue-200
rounded-lg p-4"
>
<p className="text-sm text-gray-700">
Redirecting to login page...
</p>
<div className="mt-2 flex justify-center">
<Loader2
className="animate-spin h-5 w-5
text-blue-600"
/>
</div>
</div>
<Link
to="/login"
className="inline-flex items-center
justify-center w-full py-3 px-4
border border-transparent rounded-lg
text-sm font-medium text-white
bg-indigo-600 hover:bg-indigo-700
focus:outline-none focus:ring-2
focus:ring-offset-2
focus:ring-indigo-500
transition-colors"
>
<KeyRound className="-ml-1 mr-2 h-5 w-5" />
Login Now
</Link>
</div>
) : (
// Form State
<form
onSubmit={handleSubmit(onSubmit)}
className="space-y-5"
>
{/* Error Message */}
{error && (
<div
className={`border px-4 py-3 rounded-lg
text-sm flex items-start gap-2
${
isTokenError
? 'bg-yellow-50 border-yellow-200 ' +
'text-yellow-800'
: 'bg-red-50 border-red-200 ' +
'text-red-700'
}`}
>
<AlertCircle className="h-5 w-5 flex-shrink-0 mt-0.5" />
<div className="flex-1">
<p className="font-medium">
{isReuseError
? 'New password must be different from old password'
: error}
</p>
{isTokenError && (
<Link
to="/forgot-password"
className="mt-2 inline-block text-sm
font-medium underline
hover:text-yellow-900"
>
Request new link
</Link>
)}
</div>
</div>
)}
{/* Password Field */}
<div>
<label
htmlFor="password"
className="block text-sm font-medium
text-gray-700 mb-2"
>
New Password
</label>
<div className="relative">
<div
className="absolute inset-y-0 left-0
pl-3 flex items-center
pointer-events-none"
>
<Lock
className="h-5 w-5 text-gray-400"
/>
</div>
<input
{...register('password')}
id="password"
type={showPassword ? 'text' : 'password'}
autoComplete="new-password"
autoFocus
className={`block w-full pl-10 pr-10
py-3 border rounded-lg
focus:outline-none focus:ring-2
transition-colors
${
errors.password
? 'border-red-300 ' +
'focus:ring-red-500'
: 'border-gray-300 ' +
'focus:ring-indigo-500'
}`}
placeholder="••••••••"
/>
<button
type="button"
onClick={() =>
setShowPassword(!showPassword)
}
className="absolute inset-y-0 right-0
pr-3 flex items-center"
>
{showPassword ? (
<EyeOff
className="h-5 w-5 text-gray-400
hover:text-gray-600"
/>
) : (
<Eye
className="h-5 w-5 text-gray-400
hover:text-gray-600"
/>
)}
</button>
</div>
{errors.password && (
<p className="mt-1 text-sm text-red-600">
{errors.password.message}
</p>
)}
{/* Password Strength Indicator */}
{password && password.length > 0 && (
<div className="mt-2">
<div className="flex items-center gap-2">
<div
className="flex-1 h-2 bg-gray-200
rounded-full overflow-hidden"
>
<div
className={`h-full transition-all
duration-300
${passwordStrength.color}`}
style={{
width: `${
(passwordStrength.strength / 5) *
100
}%`,
}}
/>
</div>
<span
className="text-xs font-medium
text-gray-600"
>
{passwordStrength.label}
</span>
</div>
{/* Password Requirements */}
<div className="mt-2 space-y-1">
<PasswordRequirement
met={password.length >= 8}
text="At least 8 characters"
/>
<PasswordRequirement
met={/[a-z]/.test(password)}
text="Lowercase letter (a-z)"
/>
<PasswordRequirement
met={/[A-Z]/.test(password)}
text="Uppercase letter (A-Z)"
/>
<PasswordRequirement
met={/\d/.test(password)}
text="Number (0-9)"
/>
<PasswordRequirement
met={/[@$!%*?&]/.test(password)}
text="Special character (@$!%*?&)"
/>
</div>
</div>
)}
</div>
{/* Confirm Password Field */}
<div>
<label
htmlFor="confirmPassword"
className="block text-sm font-medium
text-gray-700 mb-2"
>
Confirm Password
</label>
<div className="relative">
<div
className="absolute inset-y-0 left-0
pl-3 flex items-center
pointer-events-none"
>
<Lock
className="h-5 w-5 text-gray-400"
/>
</div>
<input
{...register('confirmPassword')}
id="confirmPassword"
type={
showConfirmPassword ? 'text' : 'password'
}
autoComplete="new-password"
className={`block w-full pl-10 pr-10
py-3 border rounded-lg
focus:outline-none focus:ring-2
transition-colors
${
errors.confirmPassword
? 'border-red-300 ' +
'focus:ring-red-500'
: 'border-gray-300 ' +
'focus:ring-indigo-500'
}`}
placeholder="••••••••"
/>
<button
type="button"
onClick={() =>
setShowConfirmPassword(
!showConfirmPassword
)
}
className="absolute inset-y-0 right-0
pr-3 flex items-center"
>
{showConfirmPassword ? (
<EyeOff
className="h-5 w-5 text-gray-400
hover:text-gray-600"
/>
) : (
<Eye
className="h-5 w-5 text-gray-400
hover:text-gray-600"
/>
)}
</button>
</div>
{errors.confirmPassword && (
<p className="mt-1 text-sm text-red-600">
{errors.confirmPassword.message}
</p>
)}
</div>
{/* Submit Button */}
<button
type="submit"
disabled={isLoading}
className="w-full flex items-center
justify-center py-3 px-4 border
border-transparent rounded-lg
shadow-sm text-sm font-medium
text-white bg-indigo-600
hover:bg-indigo-700
focus:outline-none focus:ring-2
focus:ring-offset-2
focus:ring-indigo-500
disabled:opacity-50
disabled:cursor-not-allowed
transition-colors"
>
{isLoading ? (
<>
<Loader2
className="animate-spin -ml-1 mr-2
h-5 w-5"
/>
Processing...
</>
) : (
<>
<KeyRound
className="-ml-1 mr-2 h-5 w-5"
/>
Reset Password
</>
)}
</button>
{/* Back to Login Link */}
<div className="text-center">
<Link
to="/login"
className="text-sm font-medium
text-indigo-600 hover:text-indigo-500
transition-colors"
>
Back to Login
</Link>
</div>
</form>
)}
</div>
{/* Security Info */}
{!isSuccess && (
<div
className="bg-white rounded-lg shadow-sm
border border-gray-200 p-4"
>
<h3
className="text-sm font-semibold
text-gray-900 mb-2 flex items-center
gap-2"
>
<Lock className="h-4 w-4" />
Security
</h3>
<ul
className="text-xs text-gray-600 space-y-1
list-disc list-inside"
>
<li>Reset link is valid for 1 hour only</li>
<li>Password is securely encrypted</li>
<li>
If the link expires, please request a new link
</li>
</ul>
</div>
)}
</div>
</div>
);
};
// Helper component for password requirements
const PasswordRequirement: React.FC<{
met: boolean;
text: string;
}> = ({ met, text }) => (
<div className="flex items-center gap-2 text-xs">
{met ? (
<CheckCircle2 className="h-4 w-4 text-green-500" />
) : (
<XCircle className="h-4 w-4 text-gray-300" />
)}
<span className={met ? 'text-green-600' : 'text-gray-500'}>
{text}
</span>
</div>
);
export default ResetPasswordPage;

View File

@@ -0,0 +1,4 @@
export { default as LoginPage } from './LoginPage';
export { default as RegisterPage } from './RegisterPage';
export { default as ForgotPasswordPage } from './ForgotPasswordPage';
export { default as ResetPasswordPage } from './ResetPasswordPage';