182 lines
9.4 KiB
TypeScript
182 lines
9.4 KiB
TypeScript
import { useState, useRef, FormEventHandler } from 'react';
|
|
import InputError from '@/Components/InputError';
|
|
import { Input } from '@/Components/ui/input';
|
|
import { Label } from '@/Components/ui/label';
|
|
import { Button } from '@/Components/ui/button';
|
|
import { Transition } from '@headlessui/react';
|
|
import { useForm } from '@inertiajs/react';
|
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/Components/ui/card';
|
|
import { KeyRound, Eye, EyeOff, CheckCircle2, ShieldAlert } from 'lucide-react';
|
|
|
|
export default function UpdatePasswordForm({
|
|
className = '',
|
|
}: {
|
|
className?: string;
|
|
}) {
|
|
const passwordInput = useRef<HTMLInputElement>(null);
|
|
const currentPasswordInput = useRef<HTMLInputElement>(null);
|
|
|
|
const [showCurrent, setShowCurrent] = useState(false);
|
|
const [showNew, setShowNew] = useState(false);
|
|
const [showConfirm, setShowConfirm] = useState(false);
|
|
|
|
const {
|
|
data,
|
|
setData,
|
|
errors,
|
|
put,
|
|
reset,
|
|
processing,
|
|
recentlySuccessful,
|
|
} = useForm({
|
|
current_password: '',
|
|
password: '',
|
|
password_confirmation: '',
|
|
});
|
|
|
|
const updatePassword: FormEventHandler = (e) => {
|
|
e.preventDefault();
|
|
|
|
put(route('password.update'), {
|
|
preserveScroll: true,
|
|
onSuccess: () => reset(),
|
|
onError: (errors) => {
|
|
if (errors.password) {
|
|
reset('password', 'password_confirmation');
|
|
passwordInput.current?.focus();
|
|
}
|
|
|
|
if (errors.current_password) {
|
|
reset('current_password');
|
|
currentPasswordInput.current?.focus();
|
|
}
|
|
},
|
|
});
|
|
};
|
|
|
|
return (
|
|
<Card className={`border-slate-200/80 dark:border-slate-800 shadow-xs bg-white dark:bg-slate-900 overflow-hidden ${className}`}>
|
|
<CardHeader className="bg-slate-50/70 dark:bg-slate-800/50 border-b border-slate-100 dark:border-slate-800/80 py-4 px-6">
|
|
<CardTitle className="text-base font-bold text-slate-800 dark:text-slate-100 flex items-center gap-2">
|
|
<div className="p-1.5 rounded-lg bg-indigo-50 dark:bg-indigo-950/50 text-indigo-600 dark:text-indigo-400">
|
|
<KeyRound className="h-4 w-4" />
|
|
</div>
|
|
Password & Security
|
|
</CardTitle>
|
|
<CardDescription className="text-xs text-slate-500 dark:text-slate-400">
|
|
Ensure your account is protected with a secure, randomized password.
|
|
</CardDescription>
|
|
</CardHeader>
|
|
<CardContent className="p-6">
|
|
<form onSubmit={updatePassword} className="space-y-5">
|
|
{/* Current Password */}
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="current_password" className="text-xs font-bold text-slate-700 dark:text-slate-300">
|
|
Current Password
|
|
</Label>
|
|
<div className="relative">
|
|
<Input
|
|
id="current_password"
|
|
ref={currentPasswordInput}
|
|
value={data.current_password}
|
|
onChange={(e) => setData('current_password', e.target.value)}
|
|
type={showCurrent ? 'text' : 'password'}
|
|
className="bg-white dark:bg-slate-950 text-xs border-slate-200 dark:border-slate-800 focus-visible:ring-emerald-500 focus-visible:border-emerald-500 h-9 pr-9"
|
|
autoComplete="current-password"
|
|
placeholder="••••••••"
|
|
/>
|
|
<button
|
|
type="button"
|
|
onClick={() => setShowCurrent(!showCurrent)}
|
|
className="absolute right-2.5 top-1/2 -translate-y-1/2 text-slate-400 hover:text-slate-600 dark:hover:text-slate-300 transition-colors"
|
|
>
|
|
{showCurrent ? <EyeOff className="h-4 w-4" /> : <Eye className="h-4 w-4" />}
|
|
</button>
|
|
</div>
|
|
<InputError message={errors.current_password} className="mt-1" />
|
|
</div>
|
|
|
|
{/* New & Confirm Password Grid */}
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="password" className="text-xs font-bold text-slate-700 dark:text-slate-300">
|
|
New Password
|
|
</Label>
|
|
<div className="relative">
|
|
<Input
|
|
id="password"
|
|
ref={passwordInput}
|
|
value={data.password}
|
|
onChange={(e) => setData('password', e.target.value)}
|
|
type={showNew ? 'text' : 'password'}
|
|
className="bg-white dark:bg-slate-950 text-xs border-slate-200 dark:border-slate-800 focus-visible:ring-emerald-500 focus-visible:border-emerald-500 h-9 pr-9"
|
|
autoComplete="new-password"
|
|
placeholder="••••••••"
|
|
/>
|
|
<button
|
|
type="button"
|
|
onClick={() => setShowNew(!showNew)}
|
|
className="absolute right-2.5 top-1/2 -translate-y-1/2 text-slate-400 hover:text-slate-600 dark:hover:text-slate-300 transition-colors"
|
|
>
|
|
{showNew ? <EyeOff className="h-4 w-4" /> : <Eye className="h-4 w-4" />}
|
|
</button>
|
|
</div>
|
|
<InputError message={errors.password} className="mt-1" />
|
|
</div>
|
|
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="password_confirmation" className="text-xs font-bold text-slate-700 dark:text-slate-300">
|
|
Confirm New Password
|
|
</Label>
|
|
<div className="relative">
|
|
<Input
|
|
id="password_confirmation"
|
|
value={data.password_confirmation}
|
|
onChange={(e) => setData('password_confirmation', e.target.value)}
|
|
type={showConfirm ? 'text' : 'password'}
|
|
className="bg-white dark:bg-slate-950 text-xs border-slate-200 dark:border-slate-800 focus-visible:ring-emerald-500 focus-visible:border-emerald-500 h-9 pr-9"
|
|
autoComplete="new-password"
|
|
placeholder="••••••••"
|
|
/>
|
|
<button
|
|
type="button"
|
|
onClick={() => setShowConfirm(!showConfirm)}
|
|
className="absolute right-2.5 top-1/2 -translate-y-1/2 text-slate-400 hover:text-slate-600 dark:hover:text-slate-300 transition-colors"
|
|
>
|
|
{showConfirm ? <EyeOff className="h-4 w-4" /> : <Eye className="h-4 w-4" />}
|
|
</button>
|
|
</div>
|
|
<InputError message={errors.password_confirmation} className="mt-1" />
|
|
</div>
|
|
</div>
|
|
|
|
<div className="flex items-center justify-between pt-2 border-t border-slate-100 dark:border-slate-800/80">
|
|
<Button
|
|
type="submit"
|
|
disabled={processing}
|
|
className="bg-emerald-600 hover:bg-emerald-700 text-white text-xs font-semibold h-9 px-4 shadow-2xs transition-colors"
|
|
>
|
|
Update Password
|
|
</Button>
|
|
|
|
<Transition
|
|
show={recentlySuccessful}
|
|
enter="transition ease-in-out duration-300"
|
|
enterFrom="opacity-0 translate-x-2"
|
|
enterTo="opacity-100 translate-x-0"
|
|
leave="transition ease-in-out duration-300"
|
|
leaveFrom="opacity-100"
|
|
leaveTo="opacity-0"
|
|
>
|
|
<div className="flex items-center gap-1.5 text-emerald-600 dark:text-emerald-400 text-xs font-semibold">
|
|
<CheckCircle2 className="h-4 w-4" />
|
|
<span>Password updated successfully.</span>
|
|
</div>
|
|
</Transition>
|
|
</div>
|
|
</form>
|
|
</CardContent>
|
|
</Card>
|
|
);
|
|
}
|