All files / components/auth SignInForm.tsx

94.59% Statements 35/37
86.36% Branches 19/22
100% Functions 4/4
94.59% Lines 35/37

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            1x 1x 1x               1x 1x 1x 1x 1x             1x 368x 368x 368x 368x   368x 11x 11x 11x   11x 11x         4x 2x 2x 1x 1x 1x         5x 5x 3x 2x 1x   1x           9x       368x                                       176x                                                     155x                                                                    
"use client";
 
/**
 * SignInForm - Sign in form for all user types
 * @see JCN-4
 */
import { useState } from "react";
import { signIn } from "aws-amplify/auth";
import {
  Card,
  CardContent,
  CardDescription,
  CardFooter,
  CardHeader,
  CardTitle,
} from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { Loader2, AlertCircle, LogIn } from "lucide-react";
 
interface SignInFormProps {
  onSuccess?: () => void;
  onForgotPassword?: () => void;
}
 
export function SignInForm({ onSuccess, onForgotPassword }: SignInFormProps) {
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [isLoading, setIsLoading] = useState(false);
  const [error, setError] = useState("");
 
  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault();
    setError("");
    setIsLoading(true);
 
    try {
      const { isSignedIn, nextStep } = await signIn({
        username: email,
        password,
      });
 
      if (isSignedIn) {
        onSuccess?.();
      } else if (nextStep.signInStep === "CONFIRM_SIGN_UP") {
        setError("Please verify your email first. Check your inbox for a verification code.");
      } else if (nextStep.signInStep === "RESET_PASSWORD") {
        setError("You need to reset your password. Please use the forgot password link.");
      } else E{
        setError("Additional verification required. Please contact support.");
      }
    } catch (err: unknown) {
      if (err instanceof Error) {
        if (err.name === "UserNotFoundException" || err.name === "NotAuthorizedException") {
          setError("Invalid email or password. Please try again.");
        } else if (err.name === "UserNotConfirmedException") {
          setError("Please verify your email first. Check your inbox for a verification code.");
        } else {
          setError(err.message || "An error occurred during sign in. Please try again.");
        }
      } else E{
        setError("An error occurred during sign in. Please try again.");
      }
    } finally {
      setIsLoading(false);
    }
  };
 
  return (
    <Card className="w-full max-w-md mx-auto" data-testid="signin-form">
      <CardHeader className="text-center">
        <div className="flex items-center justify-center gap-2 mb-4">
          <LogIn className="h-8 w-8 text-primary" />
        </div>
        <CardTitle>Welcome back</CardTitle>
        <CardDescription>
          Sign in to your Juncan account
        </CardDescription>
      </CardHeader>
      <form onSubmit={handleSubmit}>
        <CardContent className="space-y-4">
          <div className="space-y-2">
            <Label htmlFor="email">Email</Label>
            <Input
              id="email"
              type="email"
              data-testid="signin-email"
              value={email}
              onChange={(e) => setEmail(e.target.value)}
              placeholder="you@example.com"
              required
              disabled={isLoading}
              autoComplete="email"
            />
          </div>
          <div className="space-y-2">
            <div className="flex items-center justify-between">
              <Label htmlFor="password">Password</Label>
              {onForgotPassword && (
                <Button
                  type="button"
                  variant="link"
                  className="p-0 h-auto text-xs"
                  onClick={onForgotPassword}
                  data-testid="forgot-password-link"
                >
                  Forgot password?
                </Button>
              )}
            </div>
            <Input
              id="password"
              type="password"
              data-testid="signin-password"
              value={password}
              onChange={(e) => setPassword(e.target.value)}
              placeholder="Enter your password"
              required
              disabled={isLoading}
              autoComplete="current-password"
            />
          </div>
          {error && (
            <Alert variant="destructive" data-testid="signin-error">
              <AlertCircle className="h-4 w-4" />
              <AlertTitle>Error</AlertTitle>
              <AlertDescription>{error}</AlertDescription>
            </Alert>
          )}
        </CardContent>
        <CardFooter className="flex flex-col gap-4">
          <Button type="submit" className="w-full" disabled={isLoading} data-testid="signin-submit">
            {isLoading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
            Sign In
          </Button>
          <p className="text-sm text-center text-muted-foreground">
            Don&apos;t have an account?{" "}
            <a
              href="/signup"
              className="text-primary hover:underline font-medium"
            >
              Sign up
            </a>
          </p>
        </CardFooter>
      </form>
    </Card>
  );
}