/login, /register and /forgot-password rendered bare cards with no site header. They linked to each other and nowhere else, so a customer who clicked Log in from the storefront and changed their mind had no way back except the browser's back button. All four auth routes are now modals over the page the customer was already on, reusing the backdrop-location arrangement from #51: a direct visit or a link from an email opens over the storefront, so closing always lands somewhere real. They remain real routes, because /reset-password links to /login and customers may have bookmarks. Signing in or registering now returns the customer to the page behind, signed in, rather than moving them to /account. Someone who signs in while browsing wants to carry on browsing, and this is already how the cart and favorites prompts behave when they resume an interrupted action. The larger half of this is removing the duplication. Signing in existed twice — as these routes and again inside the prompt shown when a signed-out visitor adds to the cart or favorites something — and the two had already drifted. There were three different wordings of the marketing consent in circulation: the register page's, a shorter one in the prompt, and the string the server actually stores. The server keeps that text verbatim so the consent record says what the customer saw, which none of the three did. Both callers now render one shared AuthForm whose checkbox is the exact string the server records, and a test asserts that wording so it cannot drift again silently. Steps within the auth flow replace rather than push, so switching between tabs or stepping to password recovery leaves the whole detour as a single history entry and closing returns to where it started instead of walking back through every tab that was looked at. The privacy policy link opens in a new tab: following it in place would discard a part-filled signup form, and /privacy still has no way back of its own until #52. Test changes follow from the destination change rather than being incidental. Nineteen assertions across seven specs waited for /account after signing in; they now assert the header shows a signed-in customer, which is the condition actually being waited for. Modal submits are scoped to their dialog, because the storefront behind now offers a Log in button of its own and an unscoped locator matched both. Assertions that follow a server round-trip were given a realistic timeout — the 5s default is too tight for a bcrypt hash plus re-rendering the storefront behind the modal. Verified with 83 end-to-end tests, all passing, and type checking clean. No backend changes. Closes #50
94 lines
2.9 KiB
TypeScript
94 lines
2.9 KiB
TypeScript
import { useState } from 'react';
|
|
import Form from 'antd/es/form';
|
|
import Input from 'antd/es/input';
|
|
import Button from 'antd/es/button';
|
|
import Typography from 'antd/es/typography';
|
|
import Modal from 'antd/es/modal';
|
|
import Alert from 'antd/es/alert';
|
|
import { requestPasswordReset } from './customerApi';
|
|
|
|
const { Paragraph, Text } = Typography;
|
|
|
|
interface Props {
|
|
onClose: () => void;
|
|
// Steps back to sign-in without leaving a history entry behind, the same way
|
|
// the auth modal switches between its own tabs.
|
|
onBackToSignIn: () => void;
|
|
}
|
|
|
|
export default function ForgotPassword({ onClose, onBackToSignIn }: Props) {
|
|
const [sent, setSent] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
async function onFinish(values: { email: string }) {
|
|
setLoading(true);
|
|
setError(null);
|
|
try {
|
|
await requestPasswordReset(values.email);
|
|
setSent(true);
|
|
} catch (err) {
|
|
setError((err as Error).message);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Modal
|
|
title="Reset your password"
|
|
open
|
|
onCancel={onClose}
|
|
footer={null}
|
|
style={{ maxWidth: 'calc(100vw - 32px)' }}
|
|
destroyOnHidden
|
|
>
|
|
|
|
{sent ? (
|
|
<>
|
|
{/* Worded so it reveals nothing about whether the address has an
|
|
account — the server deliberately answers the same either way. */}
|
|
<Alert
|
|
type="success"
|
|
showIcon
|
|
message="Check your email"
|
|
description="If an account exists for that address, we've sent a link to reset the password. The link expires in one hour."
|
|
/>
|
|
<Paragraph style={{ marginTop: 16 }}>
|
|
<Button type="link" style={{ paddingInline: 0 }} onClick={onBackToSignIn}>
|
|
Back to sign in
|
|
</Button>
|
|
</Paragraph>
|
|
</>
|
|
) : (
|
|
<>
|
|
<Paragraph type="secondary">
|
|
Enter the email address for your account and we'll send you a link to choose a new password.
|
|
</Paragraph>
|
|
{error && <Alert type="error" showIcon message={error} style={{ marginBottom: 16 }} />}
|
|
<Form layout="vertical" onFinish={onFinish}>
|
|
<Form.Item
|
|
name="email"
|
|
label="Email"
|
|
rules={[{ required: true, type: 'email', message: 'Enter a valid email address' }]}
|
|
>
|
|
<Input autoComplete="email" />
|
|
</Form.Item>
|
|
<Form.Item>
|
|
<Button block type="primary" htmlType="submit" loading={loading}>
|
|
Send reset link
|
|
</Button>
|
|
</Form.Item>
|
|
</Form>
|
|
<Text type="secondary">
|
|
Remembered it?{' '}
|
|
<Button type="link" style={{ paddingInline: 0 }} onClick={onBackToSignIn}>
|
|
Sign in
|
|
</Button>
|
|
</Text>
|
|
</>
|
|
)}
|
|
</Modal>
|
|
);
|
|
}
|