From bee6281dde5ad2a07b4ac611e7745ef8636e2560 Mon Sep 17 00:00:00 2001 From: mihir Date: Thu, 26 Nov 2026 00:41:25 +0530 Subject: [PATCH] feat: let customers create an account from the sign-in page Adds a sign-up mode to /workspace that calls POST /auth/register and adopts the returned session through the same checks as sign-in. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- docs/RELEASE-CHECKLIST.md | 2 +- src/lib/api.ts | 15 +++++++++++ src/lib/platform.tsx | 13 ++++++++-- src/pages/WorkspacePage.tsx | 50 +++++++++++++++++++++++++++++++------ test/session.ui.test.tsx | 16 ++++++++++++ 6 files changed, 86 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 0cafa2f..5c11df4 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ After the one-time [server setup](docs/STATIC-DEPLOYMENT.md), main-branch Drone Storefront list returns only id/name/slug; details return prices/attributes/groups. The API has no product-media fields, so catalog cards use a generic branded placeholder rather than assigning local photos to unknown records. Existing marketing imagery remains intact. -Addresses, cart/checkout, orders, catalog, access, stock, coupons and pricing tools are available after sign-in. Public registration and procurement/production/B2B tools remain subsequent phases. There is no contact-message endpoint; simulated success was replaced with an availability notice. Newsletter signup awaits consent/messaging flows. +Addresses, cart/checkout, orders, catalog, access, stock, coupons and pricing tools are available after sign-in. Customers can create their own account at `/workspace` (requires the backend `/auth/register` endpoint). There is no contact-message endpoint; simulated success was replaced with an availability notice. Newsletter signup awaits consent/messaging flows. See [delivery phases](docs/PHASES.md) and [UAT evidence](docs/UAT-RESULTS.md). diff --git a/docs/RELEASE-CHECKLIST.md b/docs/RELEASE-CHECKLIST.md index 591f54a..acd0a5b 100644 --- a/docs/RELEASE-CHECKLIST.md +++ b/docs/RELEASE-CHECKLIST.md @@ -38,4 +38,4 @@ Record the active and previous build/commit release IDs before rollout. The SSH ## Operational limits -Pending intents exist only within the active browser session; reloading signs out and discards them. After interruption, inspect orders or the stock ledger before beginning a new intent. The backend currently has no live gateway/carrier, public registration or contact delivery. Staff catalog/role writes follow the backend's existing concurrency policy; coordinate overlapping administration edits. Never reinterpret finalized merchandise as proof of payment or fulfillment. +Pending intents exist only within the active browser session; reloading signs out and discards them. After interruption, inspect orders or the stock ledger before beginning a new intent. The backend currently has no live gateway/carrier or contact delivery; customer self-registration is available. Staff catalog/role writes follow the backend's existing concurrency policy; coordinate overlapping administration edits. Never reinterpret finalized merchandise as proof of payment or fulfillment. diff --git a/src/lib/api.ts b/src/lib/api.ts index e3a9531..1b9c149 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -235,6 +235,21 @@ export async function login( }), ); } +export async function register( + organizationId: string, + name: string, + email: string, + password: string, +) { + const organization = organizationSchema.parse(organizationId); + return parse( + sessionSchema, + await apiRequest("/auth/register", { + method: "POST", + body: { organizationId: organization, name, email, password }, + }), + ); +} export async function getPrincipal(token: string) { return parse(principalSchema, await apiRequest("/auth/me", { token })); } diff --git a/src/lib/platform.tsx b/src/lib/platform.tsx index 0a49459..a217c31 100644 --- a/src/lib/platform.tsx +++ b/src/lib/platform.tsx @@ -18,6 +18,7 @@ import { login, organizationSchema, parse, + register, type Principal, type Session, } from "./api"; @@ -29,6 +30,7 @@ const PlatformContext = createContext<{ principal: Principal | null; scope: string; signIn: (email: string, password: string) => Promise; + signUp: (name: string, email: string, password: string) => Promise; signOut: () => Promise; notice: string; clearSession: (notice?: string) => void; @@ -110,9 +112,15 @@ export function PlatformProvider({ children }: { children: ReactNode }) { /* The in-memory setting still works. */ } } - async function signIn(email: string, password: string) { + function signIn(email: string, password: string) { + return adopt(() => login(organization, email, password)); + } + function signUp(name: string, email: string, password: string) { + return adopt(() => register(organization, name, email, password)); + } + async function adopt(start: () => ReturnType) { const version = ++generation.current; - const next = await login(organization, email, password); + const next = await start(); try { const me = await getPrincipal(next.accessToken); if ( @@ -247,6 +255,7 @@ export function PlatformProvider({ children }: { children: ReactNode }) { principal, scope: principal ? `${principal.sessionId}:${accessRevision}` : "", signIn, + signUp, signOut, notice, clearSession, diff --git a/src/pages/WorkspacePage.tsx b/src/pages/WorkspacePage.tsx index aee97db..60744ae 100644 --- a/src/pages/WorkspacePage.tsx +++ b/src/pages/WorkspacePage.tsx @@ -11,20 +11,25 @@ import "./workspace.css"; import { WorkspaceNavigation } from "@/components/WorkspaceShell"; export default function WorkspacePage() { - const { organization, principal, signIn, signOut, notice } = usePlatform(); + const { organization, principal, signIn, signUp, signOut, notice } = + usePlatform(); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); + const [creating, setCreating] = useState(false); async function submit(event: FormEvent) { event.preventDefault(); const form = event.currentTarget; const fields = new FormData(form); setBusy(true); setError(null); + const name = String(fields.get("name") ?? ""); const email = String(fields.get("email")); const password = String(fields.get("password")); form.reset(); try { - await signIn(email, password); + await (creating + ? signUp(name, email, password) + : signIn(email, password)); } catch (error) { setError(error); } finally { @@ -47,7 +52,7 @@ export default function WorkspacePage() {

Account Workspace

- Sign in with your approved Mani Candles account. + Sign in, or create a customer account to shop.

@@ -68,7 +73,7 @@ export default function WorkspacePage() { ))} ) : ( -

No permissions assigned. Contact your administrator.

+

Customer account. Use your cart, addresses and orders below.

)}

Open a customer or administration tool below. @@ -89,11 +94,20 @@ export default function WorkspacePage() { backend. )} + {creating && ( + + )} @@ -101,16 +115,36 @@ export default function WorkspacePage() { label="Password" name="password" type="password" - autoComplete="current-password" + autoComplete={creating ? "new-password" : "current-password"} required - inputProps={{ maxLength: 128 }} + helperText={creating ? "At least 15 characters." : undefined} + inputProps={{ minLength: creating ? 15 : 1, maxLength: 128 }} /> + Sessions stay in memory. Reloading or closing this page requires diff --git a/test/session.ui.test.tsx b/test/session.ui.test.tsx index 3c07e35..e31c70b 100644 --- a/test/session.ui.test.tsx +++ b/test/session.ui.test.tsx @@ -332,3 +332,19 @@ test("organization change cancels private scope and removes cached data", async await waitFor(() => expect(h.result.current.scope).toBe("")); expect(h.client.getQueriesData({ queryKey: ["private"] })).toEqual([]); }); + +test("sign-up registers in the configured organization and adopts the new session", async () => { + const bodies: unknown[] = []; + vi.stubGlobal("fetch", vi.fn(async (url: string, options?: RequestInit) => { + if (url.endsWith("/auth/register")) { + bodies.push(JSON.parse(String(options?.body))); + return json(session, 201); + } + if (url.endsWith("/auth/me")) return json({ ...me, permissions: [] }); + throw new Error(`Unexpected request ${url}`); + })); + const h = harness(); + await act(() => h.result.current.signUp("Asha Buyer", "buyer@example.com", "a long enough password")); + expect(bodies).toEqual([{ organizationId: id, name: "Asha Buyer", email: "buyer@example.com", password: "a long enough password" }]); + expect(h.result.current.principal).toMatchObject({ userId: id, permissions: [] }); +}); -- 2.40.1