({ searchParams }: FakeSignInButtonProps)
| 10 | }; |
| 11 | |
| 12 | export function FakeLoginForm({ searchParams }: FakeSignInButtonProps) { |
| 13 | const fakeUserEmail = searchParams?.fakeUser as string | undefined; |
| 14 | const [email, setEmail] = useState(fakeUserEmail || ''); |
| 15 | const [isLoading, setIsLoading] = useState(false); |
| 16 | const [isVisible, setIsVisible] = useState(true); |
| 17 | const [stytchBehavior, setStytchBehavior] = useState<'default' | 'pass' | 'fail'>('default'); |
| 18 | const callbackUrl = getSignInCallbackUrl(searchParams); |
| 19 | // Track if auto-submit has been triggered to prevent double submission in React Strict Mode |
| 20 | const hasAutoSubmitted = useRef(false); |
| 21 | |
| 22 | const handleStytchBehaviorChange = useCallback((behavior: 'default' | 'pass' | 'fail') => { |
| 23 | setStytchBehavior(behavior); |
| 24 | }, []); |
| 25 | |
| 26 | const handleSubmit = useCallback( |
| 27 | async (e?: React.FormEvent) => { |
| 28 | if (e) e.preventDefault(); |
| 29 | if (!email.trim()) return; |
| 30 | |
| 31 | // Modify email based on selected Stytch behavior. |
| 32 | // The backend handler will look for these in the email address. |
| 33 | let submissionEmail = email.trim(); |
| 34 | if (stytchBehavior === 'pass') { |
| 35 | submissionEmail = submissionEmail.replace('@', '+stytchpass@'); |
| 36 | } else if (stytchBehavior === 'fail') { |
| 37 | submissionEmail = submissionEmail.replace('@', '+stytchfail@'); |
| 38 | } |
| 39 | |
| 40 | setIsLoading(true); |
| 41 | try { |
| 42 | await signIn('fake-login', { |
| 43 | email: submissionEmail, |
| 44 | callbackUrl, |
| 45 | }); |
| 46 | } catch (error) { |
| 47 | console.error('Fake login error:', error); |
| 48 | } finally { |
| 49 | setIsLoading(false); |
| 50 | } |
| 51 | }, |
| 52 | [email, callbackUrl, stytchBehavior] |
| 53 | ); |
| 54 | |
| 55 | // Auto-submit if fakeUser is provided in search params |
| 56 | // Use ref to prevent double submission in React Strict Mode |
| 57 | useEffect(() => { |
| 58 | if (fakeUserEmail && fakeUserEmail.trim() && !hasAutoSubmitted.current) { |
| 59 | hasAutoSubmitted.current = true; |
| 60 | void handleSubmit(); |
| 61 | } |
| 62 | }, [fakeUserEmail, handleSubmit]); |
| 63 | |
| 64 | if (!isVisible) { |
| 65 | return null; |
| 66 | } |
| 67 | |
| 68 | return ( |
| 69 | <div className="fixed bottom-6 left-6 w-80 overflow-visible rounded-lg border border-amber-600/50 bg-gray-900 p-4 shadow-lg"> |
nothing calls this directly
no test coverage detected