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 | 267x 267x 267x 267x 267x 267x 267x 267x 267x 267x 267x 267x 267x 212x 186x 186x 267x 19x 19x 19x 17x 15x 15x 15x 15x 10x 10x 1x 1x 1x 14x 5x 5x 3x 1x 2x 1x 1x 3x 3x 11x 19x 9x 9x 2x 15x 267x 191x 2x 2x 2x 2x 1x 1x 1x 2x 76x 6x 1x 18x 2x | import { useState, useEffect } from "react";
import { authClient } from "../../lib/auth-client";
import { authApi } from "../../lib/api";
import { isValidEmail, isValidName } from "../../lib/validation";
interface MagicLinkFormProps {
isSignUp: boolean;
invitationToken?: string;
}
export function MagicLinkForm({ isSignUp, invitationToken }: MagicLinkFormProps) {
const [name, setName] = useState("");
const [email, setEmail] = useState("");
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [sent, setSent] = useState(false);
const [resendCountdown, setResendCountdown] = useState(0);
const [isResending, setIsResending] = useState(false);
const [touched, setTouched] = useState({ name: false, email: false });
const trimmedName = name.trim();
const trimmedEmail = email.trim();
const nameError = isSignUp && touched.name && !trimmedName
? "Name is required"
: isSignUp && touched.name && trimmedName && !isValidName(trimmedName)
? "Please enter a valid name"
: undefined;
const emailError = touched.email && !trimmedEmail
? "Email is required"
: touched.email && trimmedEmail && !isValidEmail(trimmedEmail)
? "Enter a valid email"
: undefined;
useEffect(() => {
if (resendCountdown <= 0) return;
const timer = setTimeout(() => setResendCountdown((c) => c - 1), 1000);
return () => clearTimeout(timer);
}, [resendCountdown]);
const handleSend = async (e: React.FormEvent) => {
e.preventDefault();
setTouched({ name: true, email: true });
if (isSignUp && (!trimmedName || !isValidName(trimmedName))) return;
if (!trimmedEmail || !isValidEmail(trimmedEmail)) return;
setError(null);
setIsLoading(true);
try {
// For sign-in: check if email exists before sending magic link
if (!isSignUp) {
const { exists } = await authApi.checkEmail(trimmedEmail);
if (!exists) {
setError("No account found with this email. Please sign up first.");
setIsLoading(false);
return;
}
}
// For sign-up: check if email already has an account (Issue #213)
if (isSignUp) {
const emailStatus = await authApi.checkEmail(trimmedEmail);
if (emailStatus.exists) {
if (emailStatus.provider === "google") {
setError("This email is registered with Google. Use the Google button below to sign in.");
} else if (emailStatus.provider === "facebook") {
setError("This email is registered with Facebook. Use the Facebook button below to sign in.");
} else {
setError("This email already has an account. Sign in instead.");
}
setIsLoading(false);
return;
}
}
const callbackPath = invitationToken
? `/accept-invitation?token=${invitationToken}`
: "/";
await authClient.signIn.magicLink({
email: trimmedEmail,
...(isSignUp && trimmedName ? { name: trimmedName } : {}),
callbackURL: window.location.origin + callbackPath,
});
setSent(true);
setResendCountdown(60);
} catch (err) {
setError(err instanceof Error ? err.message : "Failed to send magic link");
} finally {
setIsLoading(false);
}
};
if (sent) {
return (
<div className="text-center py-4 space-y-3">
<p className="text-sm font-medium text-foreground">Check your email</p>
<p className="text-sm text-muted-foreground">
We sent a sign-in link to <span className="font-medium">{email}</span>
</p>
<div className="text-sm text-muted-foreground">
{resendCountdown > 0 ? (
<span>Resend in {resendCountdown}s</span>
) : (
<button
type="button"
disabled={isResending}
onClick={async () => {
setIsResending(true);
try {
const callbackPath = invitationToken
? `/accept-invitation?token=${invitationToken}`
: "/";
await authClient.signIn.magicLink({
email,
...(isSignUp && trimmedName ? { name: trimmedName } : {}),
callbackURL: window.location.origin + callbackPath,
});
setResendCountdown(60);
} catch {
setError("Failed to resend magic link");
setSent(false);
} finally {
setIsResending(false);
}
}}
className="text-primary hover:underline disabled:opacity-50"
>
{isResending ? "Sending..." : "Send again"}
</button>
)}
</div>
</div>
);
}
return (
<form onSubmit={handleSend} className="space-y-4" noValidate>
{isSignUp && (
<div>
<label htmlFor="ml-name" className="block text-sm font-medium text-foreground mb-1">
Name
</label>
<input
id="ml-name"
type="text"
value={name}
onChange={(e) => setName(e.target.value)}
onBlur={() => setTouched(t => ({ ...t, name: true }))}
placeholder="Your full name"
disabled={isLoading}
className={`w-full h-10 px-3 border rounded-md bg-background text-foreground text-sm focus:outline-none focus:ring-2 focus:ring-primary focus:border-primary disabled:opacity-50 ${nameError ? "border-red-500 focus:ring-red-500" : "border-input"}`}
/>
{nameError && <p className="mt-1 text-sm text-red-500">{nameError}</p>}
</div>
)}
<div>
<label htmlFor="ml-email" className="block text-sm font-medium text-foreground mb-1">
Email
</label>
<input
id="ml-email"
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
onBlur={() => setTouched(t => ({ ...t, email: true }))}
placeholder="you@example.com"
disabled={isLoading}
className={`w-full h-10 px-3 border rounded-md bg-background text-foreground text-sm focus:outline-none focus:ring-2 focus:ring-primary focus:border-primary disabled:opacity-50 ${emailError ? "border-red-500 focus:ring-red-500" : "border-input"}`}
/>
{emailError && <p className="mt-1 text-sm text-red-500">{emailError}</p>}
</div>
{error && <p role="alert" className="text-sm text-destructive">{error}</p>}
<button
type="submit"
disabled={isLoading}
className="w-full h-10 px-4 bg-primary text-primary-foreground text-sm font-medium rounded-md hover:bg-primary/90 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
>
{isLoading ? "Sending..." : "Send Magic Link"}
</button>
</form>
);
}
|