All files / components/users ResetPasswordDialog.tsx

84.21% Statements 48/57
83.78% Branches 31/37
100% Functions 7/7
85.71% Lines 48/56

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210                                                  75x 75x 75x 75x 75x 75x 75x   75x 3x 1x 1x   2x       2x       2x       2x 2x       2x 1x 1x   1x 1x     75x 6x 6x   6x 6x   6x   6x 3x   3x 2x 2x   1x     4x 4x   3x               6x 6x 6x 6x   1x   4x       75x   75x                       1x                                   4x                                                                 3x                               3x                                                
import { useState } from 'react';
import { AppDialog } from '@/components/common/AppDialog';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Alert, AlertDescription } from '@/components/ui/alert';
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
import { Loader2, AlertTriangle, Mail, Key } from 'lucide-react';
import type { User, PasswordResetRequest } from '@/services/users.service';
import { handleApiError } from '@/lib/api';
import { useToast } from '@/hooks/use-toast';
 
interface ResetPasswordDialogProps {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  user: User | null;
  onSubmit: (userId: string, data: PasswordResetRequest) => Promise<void>;
}
 
export default function ResetPasswordDialog({
  open,
  onOpenChange,
  user,
  onSubmit,
}: ResetPasswordDialogProps) {
  const { toast } = useToast();
  const [mode, setMode] = useState<'link' | 'password'>('link');
  const [password, setPassword] = useState('');
  const [confirmPassword, setConfirmPassword] = useState('');
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const [passwordError, setPasswordError] = useState<string | null>(null);
 
  const validatePassword = (): boolean => {
    if (password.length < 12) {
      setPasswordError('Password must be at least 12 characters');
      return false;
    }
    Iif (!/[A-Z]/.test(password)) {
      setPasswordError('Password must contain at least one uppercase letter');
      return false;
    }
    Iif (!/[a-z]/.test(password)) {
      setPasswordError('Password must contain at least one lowercase letter');
      return false;
    }
    Iif (!/\d/.test(password)) {
      setPasswordError('Password must contain at least one digit');
      return false;
    }
    const specialChars = /[-!@#$%^&*()_=\\[\]{};':"\\|,.<>]/;
    Iif (!specialChars.test(password)) {
      setPasswordError('Password must contain at least one special character');
      return false;
    }
    if (password !== confirmPassword) {
      setPasswordError('Passwords do not match');
      return false;
    }
    setPasswordError(null);
    return true;
  };
 
  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault();
    Iif (!user) return;
 
    setLoading(true);
    setError(null);
 
    const data: PasswordResetRequest = {};
 
    if (mode === 'link') {
      data.send_reset_link = true;
    } else {
      if (!validatePassword()) {
        setLoading(false);
        return;
      }
      data.new_password = password;
    }
 
    try {
      await onSubmit(user.id, data);
 
      toast({
        title: "Success",
        description: mode === 'link'
          ? `Password reset link sent to ${user.email}`
          : "Password updated successfully",
      });
 
      // Reset form
      setPassword('');
      setConfirmPassword('');
      setPasswordError(null);
      onOpenChange(false);
    } catch (err) {
      setError(handleApiError(err));
    } finally {
      setLoading(false);
    }
  };
 
  const isFormValid = mode === 'link' || (password && confirmPassword);
 
  return (
    <AppDialog
      open={open}
      onOpenChange={onOpenChange}
      title="Reset Password"
      description={`Choose how you want to reset the password for ${user?.email}.`}
      className="max-w-md"
      footer={
        <>
          <Button
            type="button"
            variant="outline"
            onClick={() => onOpenChange(false)}
            disabled={loading}
          >
            Cancel
          </Button>
          <Button
            type="submit"
            form="reset-password-form"
            disabled={loading || !isFormValid}
            variant={mode === 'password' ? 'destructive' : 'default'}
          >
            {loading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
            {mode === 'link' ? 'Send Reset Link' : 'Reset Password'}
          </Button>
        </>
      }
    >
      <form id="reset-password-form" onSubmit={handleSubmit}>
        <Tabs value={mode} onValueChange={(v) => setMode(v as 'link' | 'password')}>
          <TabsList className="grid w-full grid-cols-2">
            <TabsTrigger value="link" className="gap-2">
              <Mail className="h-4 w-4" />
              Send Link
            </TabsTrigger>
            <TabsTrigger value="password" className="gap-2">
              <Key className="h-4 w-4" />
              Set Directly
            </TabsTrigger>
          </TabsList>
 
          <TabsContent value="link" className="space-y-4 pt-4">
            <div className="text-sm text-balance text-muted-foreground bg-muted p-4 rounded-lg border">
              This will send a secure password reset link to <span className="font-semibold text-foreground">{user?.email}</span>.
              The user can follow the link to set their own password. The link will expire in 1 hour.
            </div>
          </TabsContent>
 
          <TabsContent value="password" className="space-y-4 pt-4">
            <Alert variant="destructive">
              <AlertTriangle className="h-4 w-4" />
              <AlertDescription>
                This will immediately change the user's password and force them to log in again on all devices.
              </AlertDescription>
            </Alert>
 
            <div className="space-y-2">
              <Label htmlFor="password">New Password *</Label>
              <Input
                id="password"
                type="password"
                value={password}
                onChange={(e) => setPassword(e.target.value)}
                placeholder="••••••••••••"
                required={mode === 'password'}
                disabled={loading}
              />
              <p className="text-xs text-muted-foreground">
                Min 12 chars, uppercase, lowercase, digit, special char
              </p>
            </div>
 
            <div className="space-y-2">
              <Label htmlFor="confirmPassword">Confirm Password *</Label>
              <Input
                id="confirmPassword"
                type="password"
                value={confirmPassword}
                onChange={(e) => setConfirmPassword(e.target.value)}
                placeholder="••••••••••••"
                required={mode === 'password'}
                disabled={loading}
              />
            </div>
 
            {passwordError && (
              <div className="text-sm text-destructive bg-destructive/10 p-3 rounded-md">
                {passwordError}
              </div>
            )}
          </TabsContent>
 
          {error && (
            <div className="text-sm text-destructive bg-destructive/10 p-3 rounded-md mt-4">
              {typeof error === 'string' ? error : JSON.stringify(error)}
            </div>
          )}
        </Tabs>
      </form>
    </AppDialog>
  );
}