create-ailk 0.35.1 → 0.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/component-catalog.json +12 -1
- package/component-catalog.md +6 -5
- package/dist/lib/env-catalog.js +8 -0
- package/dist/surfaces.js +6 -3
- package/dist/topology.d.ts +4 -4
- package/dist/topology.js +104 -60
- package/package.json +3 -3
- package/templates/.env.example +21 -0
- package/templates/apps/api/.env.example +7 -0
- package/templates/apps/api/CLAUDE.md +9 -4
- package/templates/apps/mcp/CLAUDE.md +4 -2
- package/templates/apps/web/.env.example +16 -0
- package/templates/apps/web/CLAUDE.md +46 -5
- package/templates/apps/web/app/[locale]/(auth)/__tests__/password-reset.test.tsx +2 -0
- package/templates/apps/web/app/[locale]/(auth)/sign-in/__tests__/page.test.tsx +52 -1
- package/templates/apps/web/app/[locale]/(auth)/sign-in/__tests__/remembered-method.test.ts +43 -0
- package/templates/apps/web/app/[locale]/(auth)/sign-in/__tests__/sign-in-card.test.tsx +108 -1
- package/templates/apps/web/app/[locale]/(auth)/sign-in/page.tsx +36 -3
- package/templates/apps/web/app/[locale]/(auth)/sign-in/remembered-method.ts +37 -0
- package/templates/apps/web/app/[locale]/(auth)/sign-in/sign-in-card.tsx +90 -33
- package/templates/apps/web/app/[locale]/(auth)/sign-in/sign-in-text.ts +9 -4
- package/templates/apps/web/app/[locale]/(authed)/__tests__/gate.test.tsx +79 -348
- package/templates/apps/web/app/[locale]/(authed)/account/__tests__/billing.test.tsx +12 -1
- package/templates/apps/web/app/[locale]/(authed)/account/__tests__/hub-mode.test.tsx +55 -2
- package/templates/apps/web/app/[locale]/(authed)/account/__tests__/layout-sign-out.test.tsx +10 -5
- package/templates/apps/web/app/[locale]/(authed)/account/account-frame.tsx +1 -1
- package/templates/apps/web/app/[locale]/(authed)/account/billing/page.tsx +60 -6
- package/templates/apps/web/app/[locale]/(authed)/account/billing-source.ts +32 -3
- package/templates/apps/web/app/[locale]/(authed)/account/connect/__tests__/page.test.tsx +8 -1
- package/templates/apps/web/app/[locale]/(authed)/account/connect/page.tsx +7 -3
- package/templates/apps/web/app/[locale]/(authed)/account/general/__tests__/phone-verification.test.tsx +215 -0
- package/templates/apps/web/app/[locale]/(authed)/account/general/__tests__/seam-imports.test.ts +65 -0
- package/templates/apps/web/app/[locale]/(authed)/account/general/page.tsx +47 -0
- package/templates/apps/web/app/[locale]/(authed)/account/general/phone-actions.ts +122 -0
- package/templates/apps/web/app/[locale]/(authed)/account/general/phone-verification.tsx +214 -0
- package/templates/apps/web/app/[locale]/(authed)/account/invitations/[invitationId]/__tests__/invitation-id.test.ts +35 -0
- package/templates/apps/web/app/[locale]/(authed)/account/invitations/[invitationId]/__tests__/page.test.tsx +266 -0
- package/templates/apps/web/app/[locale]/(authed)/account/invitations/[invitationId]/actions.ts +116 -0
- package/templates/apps/web/app/[locale]/(authed)/account/invitations/[invitationId]/invitation-id.ts +12 -0
- package/templates/apps/web/app/[locale]/(authed)/account/invitations/[invitationId]/page.tsx +154 -0
- package/templates/apps/web/app/[locale]/(authed)/account/layout.tsx +6 -4
- package/templates/apps/web/app/[locale]/(authed)/account/security/page.tsx +3 -1
- package/templates/apps/web/app/[locale]/(authed)/layout.tsx +15 -13
- package/templates/apps/web/app/[locale]/(authed)/signed-out-gate.generated.tsx +20 -52
- package/templates/apps/web/app/[locale]/layout.tsx +24 -75
- package/templates/apps/web/app/[locale]/purchase/__tests__/success.test.tsx +50 -0
- package/templates/apps/web/app/[locale]/purchase/success/page.tsx +48 -7
- package/templates/apps/web/app/api/auth/hub/__tests__/entry-routes.test.ts +749 -0
- package/templates/apps/web/app/api/auth/hub/authorize/__tests__/route.test.ts +166 -7
- package/templates/apps/web/app/api/auth/hub/authorize/route.ts +96 -30
- package/templates/apps/web/app/api/auth/hub/continuation.ts +51 -2
- package/templates/apps/web/app/api/auth/hub/email/route.ts +111 -0
- package/templates/apps/web/app/api/auth/hub/entry-shared.ts +163 -0
- package/templates/apps/web/app/api/auth/hub/purchase/route.ts +230 -0
- package/templates/apps/web/app/api/auth/hub/purchase-link/route.ts +174 -0
- package/templates/apps/web/app/api/auth/hub/sign-up/route.ts +60 -0
- package/templates/apps/web/app/api/auth/hub/sign-up-evidence.ts +97 -0
- package/templates/apps/web/app/api/hub-session/__tests__/routes.test.ts +475 -7
- package/templates/apps/web/app/api/hub-session/callback/route.ts +13 -47
- package/templates/apps/web/app/api/hub-session/email/route.ts +125 -0
- package/templates/apps/web/app/api/hub-session/one-tap/nonce/route.ts +49 -0
- package/templates/apps/web/app/api/hub-session/one-tap/route.ts +106 -0
- package/templates/apps/web/app/api/hub-session/open/route.ts +111 -0
- package/templates/apps/web/app/api/hub-session/sign-in/route.ts +23 -40
- package/templates/apps/web/app/api/hub-session/sign-out/route.ts +1 -10
- package/templates/apps/web/auth.ts +133 -6
- package/templates/apps/web/components/sign-out-trigger.tsx +1 -1
- package/templates/apps/web/lib/__tests__/auth-email.test.ts +141 -0
- package/templates/apps/web/lib/__tests__/auth-phone.test.ts +445 -0
- package/templates/apps/web/lib/__tests__/auth-sms.test.ts +140 -0
- package/templates/apps/web/lib/__tests__/fixtures/tsconfig.flow.json +18 -0
- package/templates/apps/web/lib/__tests__/site-header.test.ts +13 -0
- package/templates/apps/web/lib/__tests__/telemetry-scrub.test.ts +19 -1
- package/templates/apps/web/lib/auth-email.ts +62 -6
- package/templates/apps/web/lib/auth-env.ts +13 -0
- package/templates/apps/web/lib/auth-sms.ts +180 -0
- package/templates/apps/web/lib/hub-session/__tests__/hub-session.test.ts +28 -0
- package/templates/apps/web/lib/hub-session/__tests__/spoke-sign-in.test.tsx +332 -0
- package/templates/apps/web/lib/hub-session/config.ts +7 -1
- package/templates/apps/web/lib/hub-session/entry.ts +238 -0
- package/templates/apps/web/lib/hub-session/index.ts +1 -4
- package/templates/apps/web/lib/hub-session/one-tap.tsx +148 -0
- package/templates/apps/web/lib/hub-session/session.ts +120 -2
- package/templates/apps/web/lib/hub-session/spoke-sign-in.tsx +277 -0
- package/templates/apps/web/lib/site-header.ts +14 -1
- package/templates/apps/web/lib/telemetry-scrub.ts +11 -0
- package/templates/apps/web/lib/topology.generated.ts +39 -0
- package/templates/apps/web/messages/en.json +112 -20
- package/templates/apps/web/middleware.ts +34 -2
- package/templates/apps/web/security-headers.config.ts +21 -0
- package/versions.json +47 -47
- package/templates/apps/web/app/[locale]/(authed)/sign-in-gate.tsx +0 -162
- package/templates/apps/web/components/signed-in-nav.tsx +0 -79
- package/templates/apps/web/lib/hub-session/__tests__/sign-in-gate.test.tsx +0 -180
- package/templates/apps/web/lib/hub-session/sign-in-gate.tsx +0 -162
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use server";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* general/phone-actions.ts — User settings › General's phone step (#11106,
|
|
5
|
+
* spec docs/specs/11106-the-workforce-foundation-roles-by-spec.md D9).
|
|
6
|
+
*
|
|
7
|
+
* Three server actions over Better Auth's phone plugin, each a SERVER call
|
|
8
|
+
* with the request's own headers, so the plugin and its guard (`auth.ts`'s
|
|
9
|
+
* `phoneVerificationGuard`) see the person's session:
|
|
10
|
+
*
|
|
11
|
+
* - `sendPhoneCode` → `auth.api.sendPhoneNumberOTP` — texts a six-digit
|
|
12
|
+
* code through the apps/api relay;
|
|
13
|
+
* - `verifyPhoneCode` → `auth.api.verifyPhoneNumber({ updatePhoneNumber:
|
|
14
|
+
* true })` — the one writer of `phoneNumber` and `phoneNumberVerified`;
|
|
15
|
+
* - `removePhone` → `auth.api.updateUser({ phoneNumber: null })`, which
|
|
16
|
+
* the plugin turns into `phoneNumberVerified: false`.
|
|
17
|
+
*
|
|
18
|
+
* Each reaches the plugin through the generated seam's `loadPhoneApi`
|
|
19
|
+
* (`lib/topology.generated.ts`, #11138), never `auth.ts` directly, so a spoke
|
|
20
|
+
* that signs in through its hub keeps this file: there the seam answers null
|
|
21
|
+
* and every action answers `unavailable`.
|
|
22
|
+
*
|
|
23
|
+
* They take no identity argument: who comes from the session alone. Each
|
|
24
|
+
* answers `{ ok: true }` or a refusal `reason` the island maps to its own
|
|
25
|
+
* line. No log line, and no answer, carries the number or the code: a log
|
|
26
|
+
* line names the action and the plugin's error code only.
|
|
27
|
+
*
|
|
28
|
+
* Not a `*.actions.ts` file and no `*Action`-suffixed export: the phone step
|
|
29
|
+
* is a person's own act on the hub, with no MCP twin (apps/mcp's parity test
|
|
30
|
+
* reads that suffix).
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
import { headers } from "next/headers";
|
|
34
|
+
|
|
35
|
+
import { loadPhoneApi } from "../../../../../lib/topology.generated";
|
|
36
|
+
|
|
37
|
+
export type PhoneActionResult =
|
|
38
|
+
| { ok: true }
|
|
39
|
+
| {
|
|
40
|
+
ok: false;
|
|
41
|
+
reason:
|
|
42
|
+
| "invalid_number"
|
|
43
|
+
| "invalid_code"
|
|
44
|
+
| "taken"
|
|
45
|
+
| "too_many"
|
|
46
|
+
| "unverified"
|
|
47
|
+
| "signed_out"
|
|
48
|
+
| "unavailable"
|
|
49
|
+
| "failed";
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** The plugin's error code of a thrown `APIError`, when it has one. */
|
|
53
|
+
function errorCode(err: unknown): string | null {
|
|
54
|
+
const code = (err as { body?: { code?: unknown } } | null)?.body?.code;
|
|
55
|
+
return typeof code === "string" ? code : null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const REASONS: Record<string, Exclude<PhoneActionResult, { ok: true }>["reason"]> = {
|
|
59
|
+
INVALID_PHONE_NUMBER: "invalid_number",
|
|
60
|
+
INVALID_OTP: "invalid_code",
|
|
61
|
+
OTP_EXPIRED: "invalid_code",
|
|
62
|
+
OTP_NOT_FOUND: "invalid_code",
|
|
63
|
+
TOO_MANY_ATTEMPTS: "too_many",
|
|
64
|
+
PHONE_NUMBER_EXIST: "taken",
|
|
65
|
+
EMAIL_NOT_VERIFIED: "unverified",
|
|
66
|
+
SESSION_REQUIRED: "signed_out",
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
function refused(action: string, err: unknown): PhoneActionResult {
|
|
70
|
+
const code = errorCode(err);
|
|
71
|
+
console.error(`[phone-actions] ${action} refused code=${code ?? "none"}`);
|
|
72
|
+
return { ok: false, reason: (code && REASONS[code]) || "failed" };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const isText = (value: unknown, max: number): value is string =>
|
|
76
|
+
typeof value === "string" && value.length > 0 && value.length <= max;
|
|
77
|
+
|
|
78
|
+
export async function sendPhoneCode(phoneNumber: unknown): Promise<PhoneActionResult> {
|
|
79
|
+
// Action input is untrusted: the plugin's validator is the real check.
|
|
80
|
+
if (!isText(phoneNumber, 16)) return { ok: false, reason: "invalid_number" };
|
|
81
|
+
// The generated seam: null on a spoke or a build without the step.
|
|
82
|
+
const api = await loadPhoneApi();
|
|
83
|
+
if (!api) return { ok: false, reason: "unavailable" };
|
|
84
|
+
try {
|
|
85
|
+
await api.sendPhoneNumberOTP({ body: { phoneNumber }, headers: await headers() });
|
|
86
|
+
return { ok: true };
|
|
87
|
+
} catch (err) {
|
|
88
|
+
return refused("sendPhoneCode", err);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export async function verifyPhoneCode(
|
|
93
|
+
phoneNumber: unknown,
|
|
94
|
+
code: unknown,
|
|
95
|
+
): Promise<PhoneActionResult> {
|
|
96
|
+
if (!isText(phoneNumber, 16)) return { ok: false, reason: "invalid_number" };
|
|
97
|
+
if (!isText(code, 6)) return { ok: false, reason: "invalid_code" };
|
|
98
|
+
const api = await loadPhoneApi();
|
|
99
|
+
if (!api) return { ok: false, reason: "unavailable" };
|
|
100
|
+
try {
|
|
101
|
+
await api.verifyPhoneNumber({
|
|
102
|
+
body: { phoneNumber, code, updatePhoneNumber: true },
|
|
103
|
+
headers: await headers(),
|
|
104
|
+
});
|
|
105
|
+
return { ok: true };
|
|
106
|
+
} catch (err) {
|
|
107
|
+
return refused("verifyPhoneCode", err);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export async function removePhone(): Promise<PhoneActionResult> {
|
|
112
|
+
const api = await loadPhoneApi();
|
|
113
|
+
if (!api) return { ok: false, reason: "unavailable" };
|
|
114
|
+
try {
|
|
115
|
+
// The plugin's own clearing write (`phoneNumber: null` also sets
|
|
116
|
+
// `phoneNumberVerified: false`).
|
|
117
|
+
await api.updateUser({ body: { phoneNumber: null }, headers: await headers() });
|
|
118
|
+
return { ok: true };
|
|
119
|
+
} catch (err) {
|
|
120
|
+
return refused("removePhone", err);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
"use client"; // owns the number and code fields and the three action calls
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* PhoneVerification — User settings › General's phone step (#11106, spec
|
|
5
|
+
* docs/specs/11106-the-workforce-foundation-roles-by-spec.md D9).
|
|
6
|
+
*
|
|
7
|
+
* The one place a person sets their own phone. Two states:
|
|
8
|
+
*
|
|
9
|
+
* - no verified number: the number field and Send code; once a code is
|
|
10
|
+
* sent, the code field and Verify (`verifyPhoneCode`, which writes the
|
|
11
|
+
* number only after the code matches);
|
|
12
|
+
* - a verified number: the number with "Verified" and Remove.
|
|
13
|
+
*
|
|
14
|
+
* `available: false` (a build without the step) renders the not-available
|
|
15
|
+
* line, and an unverified email renders the confirm-email line, with no
|
|
16
|
+
* form. Every string arrives as a prop from the page's `account.profile.*`
|
|
17
|
+
* messages; the number and the code never leave this island except to the
|
|
18
|
+
* server actions, which log neither.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import {
|
|
22
|
+
Badge,
|
|
23
|
+
Button,
|
|
24
|
+
Field,
|
|
25
|
+
Heading,
|
|
26
|
+
InlineMessage,
|
|
27
|
+
Input,
|
|
28
|
+
Text,
|
|
29
|
+
} from "@working-theory/ui/primitives";
|
|
30
|
+
import { useRouter } from "next/navigation";
|
|
31
|
+
import { useState, useTransition, type FormEvent, type ReactElement } from "react";
|
|
32
|
+
|
|
33
|
+
import {
|
|
34
|
+
removePhone,
|
|
35
|
+
sendPhoneCode,
|
|
36
|
+
verifyPhoneCode,
|
|
37
|
+
type PhoneActionResult,
|
|
38
|
+
} from "./phone-actions";
|
|
39
|
+
|
|
40
|
+
export interface PhoneVerificationLabels {
|
|
41
|
+
title: string;
|
|
42
|
+
lead: string;
|
|
43
|
+
number: string;
|
|
44
|
+
numberHint: string;
|
|
45
|
+
sendCode: string;
|
|
46
|
+
sending: string;
|
|
47
|
+
code: string;
|
|
48
|
+
codeHint: string;
|
|
49
|
+
verify: string;
|
|
50
|
+
verifying: string;
|
|
51
|
+
verified: string;
|
|
52
|
+
remove: string;
|
|
53
|
+
sent: string;
|
|
54
|
+
unavailable: string;
|
|
55
|
+
unverifiedEmail: string;
|
|
56
|
+
errors: Record<Exclude<PhoneActionResult, { ok: true }>["reason"], string>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function PhoneVerification({
|
|
60
|
+
available,
|
|
61
|
+
emailVerified,
|
|
62
|
+
phone,
|
|
63
|
+
labels,
|
|
64
|
+
}: {
|
|
65
|
+
available: boolean;
|
|
66
|
+
emailVerified: boolean;
|
|
67
|
+
phone: { number: string; verified: boolean } | null;
|
|
68
|
+
labels: PhoneVerificationLabels;
|
|
69
|
+
}): ReactElement {
|
|
70
|
+
const router = useRouter();
|
|
71
|
+
const [number, setNumber] = useState("");
|
|
72
|
+
const [code, setCode] = useState("");
|
|
73
|
+
const [codeSent, setCodeSent] = useState(false);
|
|
74
|
+
const [error, setError] = useState<string | null>(null);
|
|
75
|
+
const [pending, startTransition] = useTransition();
|
|
76
|
+
|
|
77
|
+
const run = (action: () => Promise<PhoneActionResult>, onOk: () => void) => {
|
|
78
|
+
setError(null);
|
|
79
|
+
startTransition(async () => {
|
|
80
|
+
const result = await action();
|
|
81
|
+
if (result.ok) onOk();
|
|
82
|
+
else setError(labels.errors[result.reason]);
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const onSend = (event: FormEvent<HTMLFormElement>) => {
|
|
87
|
+
event.preventDefault();
|
|
88
|
+
run(
|
|
89
|
+
() => sendPhoneCode(number.trim()),
|
|
90
|
+
() => setCodeSent(true),
|
|
91
|
+
);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const onVerify = (event: FormEvent<HTMLFormElement>) => {
|
|
95
|
+
event.preventDefault();
|
|
96
|
+
run(
|
|
97
|
+
() => verifyPhoneCode(number.trim(), code.trim()),
|
|
98
|
+
() => {
|
|
99
|
+
setCode("");
|
|
100
|
+
setCodeSent(false);
|
|
101
|
+
router.refresh();
|
|
102
|
+
},
|
|
103
|
+
);
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const onRemove = () => run(removePhone, () => router.refresh());
|
|
107
|
+
|
|
108
|
+
let body: ReactElement;
|
|
109
|
+
if (!available) {
|
|
110
|
+
body = (
|
|
111
|
+
<Text as="p" variant="body-sm" tone="muted" data-testid="account-phone-unavailable">
|
|
112
|
+
{labels.unavailable}
|
|
113
|
+
</Text>
|
|
114
|
+
);
|
|
115
|
+
} else if (phone?.verified) {
|
|
116
|
+
body = (
|
|
117
|
+
<div className="flex flex-wrap items-center gap-3" data-testid="account-phone-verified">
|
|
118
|
+
<Text as="span" variant="body" data-testid="account-phone-number">
|
|
119
|
+
{phone.number}
|
|
120
|
+
</Text>
|
|
121
|
+
<Badge data-testid="account-phone-verified-badge">{labels.verified}</Badge>
|
|
122
|
+
<Button
|
|
123
|
+
type="button"
|
|
124
|
+
variant="ghost"
|
|
125
|
+
size="big"
|
|
126
|
+
disabled={pending}
|
|
127
|
+
onClick={onRemove}
|
|
128
|
+
data-testid="account-phone-remove"
|
|
129
|
+
>
|
|
130
|
+
{labels.remove}
|
|
131
|
+
</Button>
|
|
132
|
+
</div>
|
|
133
|
+
);
|
|
134
|
+
} else if (!emailVerified) {
|
|
135
|
+
body = (
|
|
136
|
+
<Text as="p" variant="body-sm" tone="muted" data-testid="account-phone-unverified-email">
|
|
137
|
+
{labels.unverifiedEmail}
|
|
138
|
+
</Text>
|
|
139
|
+
);
|
|
140
|
+
} else {
|
|
141
|
+
body = (
|
|
142
|
+
<div className="flex flex-col gap-4">
|
|
143
|
+
<form onSubmit={onSend} className="flex flex-col gap-3" data-testid="account-phone-send-form">
|
|
144
|
+
<Field label={labels.number} htmlFor="account-phone-number-input" helperText={labels.numberHint}>
|
|
145
|
+
<Input
|
|
146
|
+
type="tel"
|
|
147
|
+
size="big"
|
|
148
|
+
inputMode="tel"
|
|
149
|
+
autoComplete="tel"
|
|
150
|
+
value={number}
|
|
151
|
+
onChange={(e) => setNumber(e.target.value)}
|
|
152
|
+
required
|
|
153
|
+
data-testid="account-phone-number-input"
|
|
154
|
+
/>
|
|
155
|
+
</Field>
|
|
156
|
+
<Button
|
|
157
|
+
type="submit"
|
|
158
|
+
variant="secondary"
|
|
159
|
+
size="big"
|
|
160
|
+
disabled={pending || number.trim() === ""}
|
|
161
|
+
data-testid="account-phone-send"
|
|
162
|
+
>
|
|
163
|
+
{pending && !codeSent ? labels.sending : labels.sendCode}
|
|
164
|
+
</Button>
|
|
165
|
+
</form>
|
|
166
|
+
{codeSent ? (
|
|
167
|
+
<form onSubmit={onVerify} className="flex flex-col gap-3" data-testid="account-phone-verify-form">
|
|
168
|
+
<InlineMessage variant="success" role="status" data-testid="account-phone-sent">
|
|
169
|
+
{labels.sent}
|
|
170
|
+
</InlineMessage>
|
|
171
|
+
<Field label={labels.code} htmlFor="account-phone-code-input" helperText={labels.codeHint}>
|
|
172
|
+
<Input
|
|
173
|
+
type="text"
|
|
174
|
+
size="big"
|
|
175
|
+
inputMode="numeric"
|
|
176
|
+
autoComplete="one-time-code"
|
|
177
|
+
maxLength={6}
|
|
178
|
+
value={code}
|
|
179
|
+
onChange={(e) => setCode(e.target.value)}
|
|
180
|
+
required
|
|
181
|
+
data-testid="account-phone-code-input"
|
|
182
|
+
/>
|
|
183
|
+
</Field>
|
|
184
|
+
<Button
|
|
185
|
+
type="submit"
|
|
186
|
+
size="big"
|
|
187
|
+
disabled={pending || code.trim() === ""}
|
|
188
|
+
data-testid="account-phone-verify"
|
|
189
|
+
>
|
|
190
|
+
{pending ? labels.verifying : labels.verify}
|
|
191
|
+
</Button>
|
|
192
|
+
</form>
|
|
193
|
+
) : null}
|
|
194
|
+
</div>
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return (
|
|
199
|
+
<section className="flex flex-col gap-3" data-testid="account-phone-section" aria-labelledby="account-phone-title">
|
|
200
|
+
<Heading level={3} visual={4} id="account-phone-title">
|
|
201
|
+
{labels.title}
|
|
202
|
+
</Heading>
|
|
203
|
+
<Text as="p" variant="body-sm" tone="muted">
|
|
204
|
+
{labels.lead}
|
|
205
|
+
</Text>
|
|
206
|
+
{body}
|
|
207
|
+
{error ? (
|
|
208
|
+
<InlineMessage variant="error" role="alert" data-testid="account-phone-error">
|
|
209
|
+
{error}
|
|
210
|
+
</InlineMessage>
|
|
211
|
+
) : null}
|
|
212
|
+
</section>
|
|
213
|
+
);
|
|
214
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment node
|
|
3
|
+
*/
|
|
4
|
+
// `isInvitationId` (#11141): the `invite_` + uuid check the invitation page
|
|
5
|
+
// and its actions run before any call. It lives in a plain module — a
|
|
6
|
+
// synchronous export of the "use server" actions file fails `next build`.
|
|
7
|
+
|
|
8
|
+
import { readFileSync } from "node:fs";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
|
|
11
|
+
import { isInvitationId } from "../invitation-id";
|
|
12
|
+
|
|
13
|
+
describe("isInvitationId", () => {
|
|
14
|
+
it("accepts `invite_` + a lowercase uuid", () => {
|
|
15
|
+
expect(isInvitationId("invite_01920000-0000-7000-8000-000000000001")).toBe(true);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it.each([
|
|
19
|
+
"",
|
|
20
|
+
"01920000-0000-7000-8000-000000000001",
|
|
21
|
+
"invite_01920000-0000-7000-8000-00000000000",
|
|
22
|
+
"invite_01920000-0000-7000-8000-0000000000011",
|
|
23
|
+
"invite_01920000-0000-7000-8000-00000000000G",
|
|
24
|
+
"INVITE_01920000-0000-7000-8000-000000000001",
|
|
25
|
+
"invite_01920000-0000-7000-8000-000000000001/accept",
|
|
26
|
+
"../invite_01920000-0000-7000-8000-000000000001",
|
|
27
|
+
])("rejects %j", (value) => {
|
|
28
|
+
expect(isInvitationId(value)).toBe(false);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('is not a "use server" module', () => {
|
|
32
|
+
const source = readFileSync(join(__dirname, "..", "invitation-id.ts"), "utf8");
|
|
33
|
+
expect(source).not.toMatch(/^\s*["']use server["']/m);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The account invitation page (#11106, spec
|
|
3
|
+
* docs/specs/11106-the-workforce-foundation-roles-by-spec.md D5; AC-3's page
|
|
4
|
+
* bullet).
|
|
5
|
+
*
|
|
6
|
+
* - Signed out, the `(authed)` gate redirects the page to
|
|
7
|
+
* `/sign-in?next=…` (asserted on the thrown `NEXT_REDIRECT`), and the
|
|
8
|
+
* mailed URL renders `/sign-in` with the email input's value set.
|
|
9
|
+
* - Signed in: a pending invitation renders the organization, the inviter,
|
|
10
|
+
* the role and Accept — a form POST. A GET of the page reads once and
|
|
11
|
+
* writes nothing.
|
|
12
|
+
* - Accept POSTs `/v1/invitations/:id/accept` with no email in its body and
|
|
13
|
+
* returns to the page with `?joined=1`, or `?error=<code>` on a refusal.
|
|
14
|
+
* - Another address's invitation renders the refusal; a missing one is
|
|
15
|
+
* `notFound()`.
|
|
16
|
+
*
|
|
17
|
+
* The API is the boundary: `fetch` answers here. `next-intl/server` reads the
|
|
18
|
+
* real en.json with placeholders filled.
|
|
19
|
+
*/
|
|
20
|
+
import { render, screen, within } from "@testing-library/react";
|
|
21
|
+
|
|
22
|
+
const mockGetActiveProviderIds = jest.fn();
|
|
23
|
+
// eslint-disable-next-line no-restricted-syntax -- #507: auth-env.ts is a pure env-introspection module with no external boundary to mock instead; a PARTIAL mock, so every predicate the gate and /sign-in call is declared.
|
|
24
|
+
jest.mock("../../../../../../../lib/auth-env", () => ({
|
|
25
|
+
__esModule: true,
|
|
26
|
+
getActiveProviderIds: () => mockGetActiveProviderIds(),
|
|
27
|
+
isResetAvailable: () => false,
|
|
28
|
+
resetSendConfigured: () => false,
|
|
29
|
+
}));
|
|
30
|
+
|
|
31
|
+
const mockGetSession = jest.fn();
|
|
32
|
+
// eslint-disable-next-line no-restricted-syntax -- #507: auth.ts is Better Auth's server singleton (a DB/HTTP boundary); the gate reads its session and /sign-in its secret.
|
|
33
|
+
jest.mock("../../../../../../../auth", () => ({
|
|
34
|
+
__esModule: true,
|
|
35
|
+
auth: { api: { getSession: (...args: unknown[]) => mockGetSession(...args) } },
|
|
36
|
+
resolveSecret: () => "test-secret-for-the-invitation-page-suite",
|
|
37
|
+
}));
|
|
38
|
+
|
|
39
|
+
let mockRequestPathname: string | undefined;
|
|
40
|
+
jest.mock("next/headers", () => ({
|
|
41
|
+
__esModule: true,
|
|
42
|
+
headers: jest.fn(async () => {
|
|
43
|
+
const h = new Headers({ origin: "https://hub.example" });
|
|
44
|
+
if (mockRequestPathname) h.set("x-pathname", mockRequestPathname);
|
|
45
|
+
return h;
|
|
46
|
+
}),
|
|
47
|
+
cookies: jest.fn(async () => ({
|
|
48
|
+
toString: () => "better-auth.session_token=abc",
|
|
49
|
+
get: () => undefined,
|
|
50
|
+
})),
|
|
51
|
+
}));
|
|
52
|
+
|
|
53
|
+
jest.mock("next/navigation", () => ({
|
|
54
|
+
__esModule: true,
|
|
55
|
+
redirect: (url: string): never => {
|
|
56
|
+
throw new Error(`NEXT_REDIRECT ${url}`);
|
|
57
|
+
},
|
|
58
|
+
notFound: (): never => {
|
|
59
|
+
throw new Error("NEXT_NOT_FOUND");
|
|
60
|
+
},
|
|
61
|
+
}));
|
|
62
|
+
|
|
63
|
+
jest.mock("@working-theory/web-i18n", () => ({
|
|
64
|
+
__esModule: true,
|
|
65
|
+
readLocales: jest.fn().mockReturnValue(["en", "fr"]),
|
|
66
|
+
}));
|
|
67
|
+
|
|
68
|
+
jest.mock("better-auth/react", () => ({
|
|
69
|
+
__esModule: true,
|
|
70
|
+
createAuthClient: () => ({ signIn: { social: jest.fn(), email: jest.fn() } }),
|
|
71
|
+
}));
|
|
72
|
+
|
|
73
|
+
jest.mock("@working-theory/api-client", () => ({
|
|
74
|
+
__esModule: true,
|
|
75
|
+
getApiBase: () => "https://api.hub.example",
|
|
76
|
+
hubSignIn: { clientInfo: jest.fn() },
|
|
77
|
+
}));
|
|
78
|
+
|
|
79
|
+
jest.mock("next-intl/server", () => {
|
|
80
|
+
const messages = jest.requireActual("../../../../../../../messages/en.json") as Record<string, unknown>;
|
|
81
|
+
const lookup = (namespace: string, key: string): unknown =>
|
|
82
|
+
`${namespace}.${key}`
|
|
83
|
+
.split(".")
|
|
84
|
+
.reduce<unknown>(
|
|
85
|
+
(node, part) => (node && typeof node === "object" ? (node as Record<string, unknown>)[part] : undefined),
|
|
86
|
+
messages,
|
|
87
|
+
);
|
|
88
|
+
return {
|
|
89
|
+
__esModule: true,
|
|
90
|
+
getTranslations: async ({ namespace }: { namespace: string }) => {
|
|
91
|
+
const t = (key: string, values?: Record<string, string>): string => {
|
|
92
|
+
const found = lookup(namespace, key);
|
|
93
|
+
return (typeof found === "string" ? found : `${namespace}.${key}`).replace(
|
|
94
|
+
/\{(\w+)\}/g,
|
|
95
|
+
(match, name: string) => values?.[name] ?? match,
|
|
96
|
+
);
|
|
97
|
+
};
|
|
98
|
+
t.has = (key: string): boolean => typeof lookup(namespace, key) === "string";
|
|
99
|
+
return t;
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
import en from "../../../../../../../messages/en.json";
|
|
105
|
+
import SignInPage from "../../../../../(auth)/sign-in/page";
|
|
106
|
+
import AuthedLayout from "../../../../layout";
|
|
107
|
+
import { acceptInvitation } from "../actions";
|
|
108
|
+
import InvitationPage from "../page";
|
|
109
|
+
|
|
110
|
+
const ID = "invite_01920000-0000-7000-8000-000000000001";
|
|
111
|
+
const ADDRESS = "invitee@example.test";
|
|
112
|
+
|
|
113
|
+
type Call = { url: string; method: string; body: unknown; headers: Record<string, string> };
|
|
114
|
+
const calls: Call[] = [];
|
|
115
|
+
let answer: { status: number; body: unknown } = { status: 200, body: null };
|
|
116
|
+
|
|
117
|
+
const realFetch = globalThis.fetch;
|
|
118
|
+
beforeEach(() => {
|
|
119
|
+
calls.length = 0;
|
|
120
|
+
mockRequestPathname = undefined;
|
|
121
|
+
mockGetActiveProviderIds.mockReset().mockReturnValue(["google", "email", "magic-link"]);
|
|
122
|
+
mockGetSession.mockReset().mockResolvedValue({ user: { email: ADDRESS, emailVerified: true } });
|
|
123
|
+
globalThis.fetch = jest.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
124
|
+
calls.push({
|
|
125
|
+
url: String(input),
|
|
126
|
+
method: init?.method ?? "GET",
|
|
127
|
+
body: init?.body ?? null,
|
|
128
|
+
headers: (init?.headers ?? {}) as Record<string, string>,
|
|
129
|
+
});
|
|
130
|
+
// A minimal answer: jsdom's global scope has no `Response`.
|
|
131
|
+
const { status, body } = answer;
|
|
132
|
+
// eslint-disable-next-line no-restricted-syntax -- the fetch boundary's double: the actions read only `status` and `json()`, and jsdom has no Response to build a real one.
|
|
133
|
+
return { status, json: async () => body } as unknown as Response;
|
|
134
|
+
}) as typeof fetch;
|
|
135
|
+
});
|
|
136
|
+
afterAll(() => {
|
|
137
|
+
globalThis.fetch = realFetch;
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
const pending = {
|
|
141
|
+
ok: true,
|
|
142
|
+
data: {
|
|
143
|
+
organizationName: "Acme Studio",
|
|
144
|
+
inviterName: "Sam Rivera",
|
|
145
|
+
role: "editor",
|
|
146
|
+
status: "pending",
|
|
147
|
+
expiresAt: new Date(Date.now() + 86_400_000).toISOString(),
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
async function renderInvitation(search: Record<string, string> = {}): Promise<void> {
|
|
152
|
+
const el = await InvitationPage({
|
|
153
|
+
params: Promise.resolve({ locale: "en", invitationId: ID }),
|
|
154
|
+
searchParams: Promise.resolve(search),
|
|
155
|
+
});
|
|
156
|
+
render(el);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
describe("signed out", () => {
|
|
160
|
+
it("the (authed) gate redirects the page to /sign-in?next=…", async () => {
|
|
161
|
+
mockGetSession.mockResolvedValue(null);
|
|
162
|
+
mockRequestPathname = `/account/invitations/${ID}`;
|
|
163
|
+
await expect(
|
|
164
|
+
AuthedLayout({ children: null, params: Promise.resolve({ locale: "en" }) }),
|
|
165
|
+
).rejects.toThrow(`NEXT_REDIRECT /sign-in?next=${encodeURIComponent(`/account/invitations/${ID}`)}`);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("the mailed URL renders /sign-in with the invited address in the email input", async () => {
|
|
169
|
+
const mailed = new URL(
|
|
170
|
+
`https://hub.example/sign-in?next=%2Faccount%2Finvitations%2F${ID}&email=${encodeURIComponent(ADDRESS)}`,
|
|
171
|
+
);
|
|
172
|
+
const el = await SignInPage({
|
|
173
|
+
params: Promise.resolve({ locale: "en" }),
|
|
174
|
+
searchParams: Promise.resolve(Object.fromEntries(mailed.searchParams)),
|
|
175
|
+
});
|
|
176
|
+
render(el);
|
|
177
|
+
expect(mailed.pathname).toBe("/sign-in");
|
|
178
|
+
expect(screen.getByTestId("auth-email")).toHaveValue(ADDRESS);
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
describe("signed in", () => {
|
|
183
|
+
it("a pending invitation renders the organization, inviter and role, and Accept is a form POST", async () => {
|
|
184
|
+
answer = { status: 200, body: pending };
|
|
185
|
+
await renderInvitation();
|
|
186
|
+
expect(screen.getByTestId("account-invitation-lead")).toHaveTextContent(
|
|
187
|
+
"Sam Rivera invited you to join Acme Studio as an editor.",
|
|
188
|
+
);
|
|
189
|
+
const form = screen.getByTestId("account-invitation-form");
|
|
190
|
+
expect(form.tagName).toBe("FORM");
|
|
191
|
+
expect(within(form).getByTestId("account-invitation-accept")).toHaveAttribute("type", "submit");
|
|
192
|
+
expect(within(form).getByTestId("account-invitation-accept")).toHaveTextContent(en.account.invitation.accept);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("a GET of the page reads once and writes nothing", async () => {
|
|
196
|
+
answer = { status: 200, body: pending };
|
|
197
|
+
await renderInvitation();
|
|
198
|
+
expect(calls).toHaveLength(1);
|
|
199
|
+
expect(calls[0]).toMatchObject({ url: `https://api.hub.example/v1/invitations/${ID}`, method: "GET" });
|
|
200
|
+
expect(calls[0]!.headers.cookie).toBe("better-auth.session_token=abc");
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("another address's invitation renders the refusal", async () => {
|
|
204
|
+
answer = {
|
|
205
|
+
status: 403,
|
|
206
|
+
body: { ok: false, error: { code: "invitation_for_another_address", message: "Forbidden" } },
|
|
207
|
+
};
|
|
208
|
+
await renderInvitation();
|
|
209
|
+
expect(screen.getByTestId("account-invitation-other-address")).toHaveTextContent(
|
|
210
|
+
"This invitation is for a different email address. Sign out and sign in with the invited address.",
|
|
211
|
+
);
|
|
212
|
+
expect(screen.queryByTestId("account-invitation-form")).toBeNull();
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it("a missing invitation, or a malformed id, is notFound()", async () => {
|
|
216
|
+
answer = { status: 404, body: { ok: false, error: { code: "not_found", message: "Not found" } } };
|
|
217
|
+
await expect(renderInvitation()).rejects.toThrow("NEXT_NOT_FOUND");
|
|
218
|
+
calls.length = 0;
|
|
219
|
+
await expect(
|
|
220
|
+
InvitationPage({
|
|
221
|
+
params: Promise.resolve({ locale: "en", invitationId: "../../admin" }),
|
|
222
|
+
searchParams: Promise.resolve({}),
|
|
223
|
+
}),
|
|
224
|
+
).rejects.toThrow("NEXT_NOT_FOUND");
|
|
225
|
+
expect(calls).toHaveLength(0);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("after joining it reads You've joined <organization>.", async () => {
|
|
229
|
+
answer = { status: 200, body: { ok: true, data: { ...pending.data, status: "accepted" } } };
|
|
230
|
+
await renderInvitation({ joined: "1" });
|
|
231
|
+
expect(screen.getByTestId("account-invitation-joined")).toHaveTextContent("You've joined Acme Studio.");
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("a refusal returned by accept shows its line beside the form", async () => {
|
|
235
|
+
answer = { status: 200, body: pending };
|
|
236
|
+
await renderInvitation({ error: "invitation_expired" });
|
|
237
|
+
expect(screen.getByTestId("account-invitation-error")).toHaveTextContent(en.account.invitation.expired);
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
describe("acceptInvitation", () => {
|
|
242
|
+
it("POSTs accept with the session and no email, then returns with ?joined=1", async () => {
|
|
243
|
+
answer = { status: 200, body: { ok: true, data: { status: "accepted", organizationName: "Acme Studio", role: "editor" } } };
|
|
244
|
+
await expect(acceptInvitation(ID, "en")).rejects.toThrow(`NEXT_REDIRECT /account/invitations/${ID}?joined=1`);
|
|
245
|
+
expect(calls).toHaveLength(1);
|
|
246
|
+
expect(calls[0]).toMatchObject({ url: `https://api.hub.example/v1/invitations/${ID}/accept`, method: "POST", body: "{}" });
|
|
247
|
+
expect(calls[0]!.headers.origin).toBe("https://hub.example");
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
it.each([
|
|
251
|
+
["invitation_expired", 409],
|
|
252
|
+
["invitation_for_another_address", 403],
|
|
253
|
+
["invitation_void", 409],
|
|
254
|
+
])("a %s refusal returns with ?error=%s", async (code, status) => {
|
|
255
|
+
answer = { status, body: { ok: false, error: { code, message: "x" } } };
|
|
256
|
+
await expect(acceptInvitation(ID, "en")).rejects.toThrow(`NEXT_REDIRECT /account/invitations/${ID}?error=${code}`);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it("anything else, or a malformed id, returns ?error=failed", async () => {
|
|
260
|
+
answer = { status: 500, body: null };
|
|
261
|
+
await expect(acceptInvitation(ID, "en")).rejects.toThrow(`?error=failed`);
|
|
262
|
+
calls.length = 0;
|
|
263
|
+
await expect(acceptInvitation("nope", "en")).rejects.toThrow(`?error=failed`);
|
|
264
|
+
expect(calls).toHaveLength(0);
|
|
265
|
+
});
|
|
266
|
+
});
|