diff --git a/src/components/LoginView.tsx b/src/components/LoginView.tsx index 6533485..b3ce6be 100644 --- a/src/components/LoginView.tsx +++ b/src/components/LoginView.tsx @@ -57,8 +57,13 @@ export default function LoginView({ onLogin }: LoginViewProps) { setError(''); setCheckingEmail(true); try { - const emailExists = await checkEmailRequest(email); - if (emailExists) { + const result = await checkEmailRequest(email); + if (result.exists && result.needsSetup && result.userid != null) { + // No point asking for a password this account doesn't have yet — + // go straight to creating one. + setSetupUserId(result.userid); + setStep('setup'); + } else if (result.exists) { setStep('password'); } else { setError('Email not found. Please try again.'); diff --git a/src/services/auth.ts b/src/services/auth.ts index c032cd2..869e2bd 100644 --- a/src/services/auth.ts +++ b/src/services/auth.ts @@ -204,11 +204,20 @@ export async function loginRequest(email: string, password: string): Promise { +export async function checkEmailRequest(email: string): Promise { const trimmedEmail = email.trim(); let res: Response; @@ -226,16 +235,24 @@ export async function checkEmailRequest(email: string): Promise { } const json = (await res.json().catch(() => null)) as - | { code?: number; status?: boolean; message?: string } + | { code?: number; status?: boolean; message?: string; details?: unknown } | null; + // Account exists but has no password yet — fires regardless of the + // (absent) password in this probe request. See loginRequest's identical + // check for the full explanation. + const details = json?.details as { userid?: number; setup?: boolean } | undefined; + if (json?.code === 409 && details?.setup === true && typeof details.userid === 'number') { + return { exists: true, needsSetup: true, userid: details.userid }; + } + // A 409 Invalid Email code means the email does not exist. if (json && json.status === false && (json.code === 409 || json.message?.trim().toLowerCase() === 'invalid email')) { - return false; + return { exists: false, needsSetup: false }; } // Any other result (such as 403 Unauthorized email) means the email is registered. - return true; + return { exists: true, needsSetup: false }; }