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 | 1x 1x 1x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 12x 2x 2x 2x 2x 37x 37x 6x 6x 6x 6x 2x 6x 4x 1x 6x 4x 2x 2x 1x 1x 1x 1x 4x 6x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 10x 37x 37x 37x 3x 3x 3x 37x 37x 37x 37x 37x 37x 37x 4x 4x 4x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 35x 10x 25x 37x 37x 37x 37x 37x 37x | import React, { useState, useEffect } from "react";
import { useNavigate, useLocation } from "react-router-dom";
import { useAuth } from "../hooks/useAuth";
export default function Login() {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [isSignUp, setIsSignUp] = useState(false);
const [error, setError] = useState("");
const navigate = useNavigate();
const location = useLocation();
const { login, register, isLoggingIn, isRegistering, isAuthenticated } =
useAuth();
interface LocationState {
from?: {
pathname: string;
};
}
// Redirect if already logged in
useEffect(() => {
if (isAuthenticated) {
const state = location.state as LocationState;
const from = state?.from?.pathname || "/dashboard";
navigate(from, { replace: true });
}
}, [isAuthenticated, navigate, location]);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setError("");
try {
if (isSignUp) {
await register([email, password]);
} else {
await login([email, password]);
}
// Navigation is handled by the useEffect above once isAuthenticated flips to true
} catch (err: unknown) {
// Check if it's an instance of Error
if (err instanceof Error) {
setError(err.message);
} else if (typeof err === "string") {
setError(err);
} else {
setError("Authentication failed. Please check your credentials.");
}
}
};
const isLoading = isLoggingIn || isRegistering;
return (
<div className="flex min-h-screen items-center justify-center bg-gray-50 px-4 py-12 sm:px-6 lg:px-8">
<div className="w-full max-w-md space-y-8">
<div>
<h2 className="text-center text-3xl font-bold tracking-tight text-gray-900">
{isSignUp ? "Create your account" : "Sign in to your account"}
</h2>
<p className="mt-2 text-center text-sm text-gray-600">
{isSignUp
? "Already have an account?"
: "Don't have an account yet?"}{" "}
<button
onClick={() => {
setIsSignUp(!isSignUp);
setError("");
}}
className="cursor-pointer font-medium text-blue-600 hover:text-blue-500"
>
{isSignUp ? "Sign in" : "Sign up"}
</button>
</p>
</div>
<form className="mt-8 space-y-6" onSubmit={handleSubmit}>
{error && (
<div className="rounded-md bg-red-50 p-4">
<p className="text-sm text-red-700">{error}</p>
</div>
)}
<div className="-space-y-px rounded-md shadow-sm">
<div>
<label htmlFor="email" className="sr-only">
Email address
</label>
<input
id="email"
name="email"
type="email"
autoComplete="email"
required
value={email}
onChange={(e) => setEmail(e.target.value)}
className="relative block w-full rounded-t-md border border-gray-300 px-3 py-2 text-gray-900 placeholder-gray-500 focus:z-10 focus:border-blue-500 focus:outline-none focus:ring-blue-500 sm:text-sm"
placeholder="Email address"
/>
</div>
<div>
<label htmlFor="password" className="sr-only">
Password
</label>
<input
id="password"
name="password"
type="password"
autoComplete="current-password"
required
value={password}
onChange={(e) => setPassword(e.target.value)}
className="relative block w-full rounded-b-md border border-gray-300 px-3 py-2 text-gray-900 placeholder-gray-500 focus:z-10 focus:border-blue-500 focus:outline-none focus:ring-blue-500 sm:text-sm"
placeholder="Password"
/>
</div>
</div>
<div>
<button
type="submit"
disabled={isLoading}
className={`cursor-pointer group relative flex w-full justify-center rounded-md border border-transparent py-2 px-4 text-sm font-medium text-white transition-colors
${isLoading ? "bg-blue-400 cursor-not-allowed" : "bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2"}
`}
>
{isLoading ? (
<span className="flex items-center">
<svg
className="mr-2 h-4 w-4 animate-spin text-white"
fill="none"
viewBox="0 0 24 24"
>
<circle
className="opacity-25"
cx="12"
cy="12"
r="10"
stroke="currentColor"
strokeWidth="4"
/>
<path
className="opacity-75"
fill="currentColor"
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
/>
</svg>
Processing...
</span>
) : isSignUp ? (
"Sign Up"
) : (
"Sign In"
)}
</button>
</div>
</form>
</div>
</div>
);
}
|