All files / src/web/pages Login.tsx

97.35% Statements 221/227
89.47% Branches 34/38
100% Functions 5/5
97.35% Lines 221/227

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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 2881x 1x 1x                 1x 1x               1x 77x 77x 77x 77x 77x 77x 77x 77x     77x 77x 77x 77x   77x 77x 77x 77x   77x 2x   2x 2x     2x 2x     2x   77x   77x       77x 77x   77x 77x 77x 77x 77x 77x   77x 77x 77x 77x 77x   77x 77x   77x 77x 77x 77x 77x 77x   77x 31x 31x 31x 31x     77x 77x 77x 77x 77x 77x   77x 77x 77x 77x 77x 77x 77x 77x 77x 77x 77x 77x 77x 77x 77x 77x 77x 77x   77x 77x 77x 77x 77x 77x   77x 77x 5x 5x 5x 5x   5x   77x 77x 77x 77x 77x 77x 77x 77x 77x 77x 77x 77x 77x 77x 77x 77x 77x 77x     77x 28x 28x   28x 28x 28x 140x 140x 86x   54x   140x 140x   140x 140x 140x 28x 28x 28x   77x   77x 77x 77x 77x 77x 77x   77x 6x   71x 71x 71x 71x   77x 77x 77x   77x 77x 77x 77x 77x 77x 77x 77x 77x 72x 5x 77x 77x 77x   77x 77x 77x 77x 2x 2x 77x   77x 72x 5x 77x 77x 77x   77x 77x     77x 77x 77x 77x   77x 77x 77x   77x 77x 77x 77x 77x 77x 77x 77x 77x 385x 385x 385x 385x 385x 385x   385x 385x 385x 385x 385x   385x 385x 385x 385x 385x 385x 385x 385x 385x 77x 77x 77x 77x     77x 77x 77x 77x   77x   1x  
import { useAuth } from "@common/contexts/AuthContext";
import { motion } from "framer-motion";
import {
  AlertCircle,
  ArrowRight,
  Check,
  Inbox,
  Lock,
  Mail,
  X,
} from "lucide-react";
import { useMemo, useState } from "react";
import { Link, Navigate, useLocation } from "react-router-dom";
 
type LocationState = {
  from?: {
    pathname: string;
  };
};
 
const Login = () => {
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [isSignUp, setIsSignUp] = useState(false);
  const { signIn, signUp, user, loading, error, checkPasswordStrength } =
    useAuth();
  const location = useLocation();
  const locationState = location.state as LocationState;
  const from = locationState?.from?.pathname || "/inbox";
 
  // Check password strength and show requirements
  const passwordRequirements = useMemo(
    () => checkPasswordStrength(password),
    [password, checkPasswordStrength],
  );
 
  const isPasswordStrong = useMemo(
    () => passwordRequirements.every((req) => req.satisfied),
    [passwordRequirements],
  );
 
  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault();
 
    if (isSignUp) {
      if (!isPasswordStrong) {
        return; // Don't proceed if password doesn't meet requirements
      }
      await signUp(email, password);
    } else {
      await signIn(email, password);
    }
  };
 
  const isProduction = import.meta.env.MODE === 'production';
 
  if (user) {
    return <Navigate to={from} replace />;
  }
 
  return (
    <div className="min-h-screen bg-gray-50 flex flex-col md:flex-row">
      {/* Left side - Form */}
      <div className="w-full md:w-1/2 min-h-screen flex items-center justify-center p-6 bg-white">
        <motion.div
          initial={{ opacity: 0, y: 20 }}
          animate={{ opacity: 1, y: 0 }}
          transition={{ duration: 0.5 }}
          className="w-full max-w-md"
        >
          <div className="flex items-center justify-center mb-8">
            <div className="bg-primary-500 p-2 rounded-lg mr-3">
              <Inbox className="text-white" size={24} />
            </div>
            <h1 className="text-2xl font-bold bg-gradient-to-r from-slate-800 to-slate-600 bg-clip-text text-transparent">
              NewsletterHub
            </h1>
          </div>
 
          <h2 className="text-2xl font-bold text-center text-slate-900 mb-3">
            {isSignUp ? "Create an account" : "Welcome back"}
          </h2>
          <p className="text-center text-slate-600 mb-10">
            {isSignUp ? "Sign up to get started" : "Sign in to your account"}
          </p>
 
          {error && (
            <div className="mb-6 p-4 bg-red-50 rounded-md flex items-start">
              <AlertCircle className="h-5 w-5 text-red-400 mt-0.5 mr-3 flex-shrink-0" />
              <p className="text-sm text-red-700">{error}</p>
            </div>
          )}
 
          <form onSubmit={handleSubmit} className="space-y-7">
            <div>
              <label
                htmlFor="email"
                className="block text-sm font-medium text-gray-700 mb-1"
              >
                Email address
              </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-slate-400" />
                </div>
                <input
                  id="email"
                  type="email"
                  value={email}
                  onChange={(e) => setEmail(e.target.value)}
                  required
                  className="input-field pr-4 py-3"
                  style={{ paddingLeft: '2.5rem' }}
                  placeholder="you@example.com"
                  data-testid="email-input"
                />
              </div>
            </div>
 
            <div>
              <div className="flex items-center justify-between mb-1">
                <label
                  htmlFor="password"
                  className="block text-sm font-medium text-gray-700"
                >
                  Password
                </label>
                {!isSignUp && (
                  <Link
                    to="/forgot-password"
                    className="text-sm font-medium text-primary-600 hover:text-primary-500"
                  >
                    Forgot password?
                  </Link>
                )}
              </div>
              <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-slate-400" />
                </div>
                <input
                  id="password"
                  type="password"
                  value={password}
                  onChange={(e) => setPassword(e.target.value)}
                  required
                  className="input-field pr-4 py-3"
                  style={{ paddingLeft: '2.5rem' }}
                  placeholder={isSignUp ? "Create a password" : "••••••••"}
                  minLength={8}
                  data-testid="password-input"
                />
              </div>
 
              {/* Password strength indicator */}
              {isSignUp && password && (
                <div className="mt-2 space-y-1">
                  <p className="text-xs text-gray-500">
                    Password must contain:
                  </p>
                  <ul className="space-y-1">
                    {passwordRequirements.map((req, i) => (
                      <li key={i} className="flex items-center">
                        {req.satisfied ? (
                          <Check className="h-3.5 w-3.5 text-green-500 mr-2" />
                        ) : (
                          <X className="h-3.5 w-3.5 text-gray-400 mr-2" />
                        )}
                        <span
                          className={`text-xs ${req.satisfied ? "text-green-600" : "text-gray-500"}`}
                        >
                          {req.label}
                        </span>
                      </li>
                    ))}
                  </ul>
                </div>
              )}
            </div>
 
            <div>
              <button
                type="submit"
                disabled={loading || (isSignUp && !isPasswordStrong)}
                className="btn btn-primary btn-lg w-full"
                data-testid="login-button"
              >
                {loading ? (
                  <span className="inline-block w-5 h-5 border-2 border-white border-t-transparent rounded-full animate-spin"></span>
                ) : (
                  <span className="flex items-center">
                    {isSignUp ? "Create account" : "Sign in"}
                    <ArrowRight className="ml-2 h-4 w-4" />
                  </span>
                )}
              </button>
            </div>
          </form>
 
          {!isProduction && (
            <div className="mt-6">
              <div className="relative">
                <div className="absolute inset-0 flex items-center">
                  <div className="w-full border-t border-gray-300"></div>
                </div>
                <div className="relative flex justify-center text-sm">
                  <span className="px-2 bg-white text-gray-500">
                    {isSignUp
                      ? "Already have an account?"
                      : "Don't have an account?"}
                  </span>
                </div>
              </div>
 
              <div className="mt-6">
                <button
                  type="button"
                  onClick={() => {
                    setIsSignUp(!isSignUp);
                  }}
                  className="btn btn-secondary btn-lg w-full"
                >
                  {isSignUp
                    ? "Sign in to existing account"
                    : "Create a new account"}
                </button>
              </div>
            </div>
          )}
        </motion.div>
      </div>
 
      {/* Right side - Decorative */}
      <div className="hidden md:flex md:w-1/2 bg-gradient-to-br from-primary-600 to-primary-800 relative overflow-hidden">
        <div className="absolute inset-0 opacity-10">
          <div className="absolute inset-0 bg-[url('data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNjAiIGhlaWdodD0iNjAiIHZpZXdCb3g9IjAgMCA2MCA2MCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KICA8cGF0aCBkPSJNNTQuMDAxIDU0Ljk5OUw1LjAwMSA1NC45OTlMNS4wMDEgNS4wMDFMNTQuMDAxIDUuMDAxTDU0LjAwMSA1NC45OTlaIiBzdHJva2U9IiNmZmYiIHN0cm9rZS13aWR0aD0iMiIgZmlsbD0ibm9uZSIgc3Ryb2tlLWRhc2hhcnJheT0iMTAiIC8+Cjwvc3ZnPg==')] bg-repeat"></div>
        </div>
 
        <div className="relative z-10 p-12 flex flex-col h-full justify-center text-white">
          <div className="max-w-md">
            <h2 className="text-3xl font-bold mb-6">
              All your newsletters in one place
            </h2>
            <ul className="space-y-4">
              {[
                "Subscribe with unique email addresses",
                "Get AI-powered summaries",
                "Search all your content semantically",
                "Listen to newsletters via text-to-speech",
                "Discover related topics across newsletters",
              ].map((feature, index) => (
                <motion.li
                  key={index}
                  initial={{ opacity: 0, x: -20 }}
                  animate={{ opacity: 1, x: 0 }}
                  transition={{ delay: 0.1 * index, duration: 0.3 }}
                  className="flex items-start"
                >
                  <svg
                    className="h-5 w-5 text-primary-200 mr-3 mt-0.5 flex-shrink-0"
                    fill="none"
                    viewBox="0 0 24 24"
                    stroke="currentColor"
                  >
                    <path
                      strokeLinecap="round"
                      strokeLinejoin="round"
                      strokeWidth={2}
                      d="M5 13l4 4L19 7"
                    />
                  </svg>
                  <span className="text-primary-100">{feature}</span>
                </motion.li>
              ))}
            </ul>
          </div>
        </div>
 
        {/* Decorative elements */}
        <div className="absolute bottom-0 right-0 w-64 h-64 bg-primary-500 rounded-full -mb-32 -mr-32 opacity-20"></div>
        <div className="absolute top-0 left-0 w-32 h-32 bg-primary-400 rounded-full -mt-16 -ml-16 opacity-30"></div>
      </div>
    </div>
  );
};
 
export default Login;