Line data Source code
1 : /**
2 : * The account-creation sequences both hosts must agree on, with the effects
3 : * injected so the invariants live (and are tested) exactly once. Mirrors the
4 : * platform app's `authStore.register` / `loginWithGoogle` semantics.
5 : */
6 :
7 : interface DeletableUserCredential {
8 : user: { delete: () => Promise<void> }
9 : }
10 :
11 : /**
12 : * Email sign-up: create the Firebase user, then provision the Comfy customer.
13 : *
14 : * Provisioning is where a Turnstile token gets validated server-side; if it
15 : * fails (rejection, 5xx, network) the Firebase user is already created and,
16 : * without rollback, the account is orphaned — every retry then fails
17 : * "email already in use", permanently bricking signup. So a provisioning
18 : * failure deletes the just-created user (best-effort with one retried
19 : * delete, so a single transient blip cannot orphan the account; a cleanup
20 : * failure never masks the original error) and rethrows.
21 : */
22 0 : export async function signUpWithProvisioning<
23 : T extends DeletableUserCredential
24 : >(deps: {
25 : createUser: () => Promise<T>
26 : provisionCustomer: (credential: T) => Promise<unknown>
27 : onRollbackFailure?: (error: unknown) => void
28 : }): Promise<T> {
29 0 : const credential = await deps.createUser()
30 0 : try {
31 0 : await deps.provisionCustomer(credential)
32 : } catch (error) {
33 0 : try {
34 0 : try {
35 0 : await credential.user.delete()
36 : } catch {
37 0 : await credential.user.delete()
38 : }
39 : } catch (rollbackError) {
40 : // A reporting sink can throw; never let it displace the original error.
41 0 : try {
42 0 : deps.onRollbackFailure?.(rollbackError)
43 : } catch {
44 : void 0
45 : }
46 : }
47 0 : throw error
48 : }
49 0 : return credential
50 : }
51 :
52 : /**
53 : * Social sign-in: the popup, then find-or-create the Comfy customer — always,
54 : * because a social user's first touch may be this host and skipping the
55 : * customer record produces accounts where billing surfaces fail.
56 : * `provisionCustomer` must tolerate an already-provisioned account. No
57 : * rollback here: the signed-in user may be a long-standing account, and
58 : * deleting it over a provisioning hiccup would destroy it — a provisioning
59 : * failure propagates with the user still signed in.
60 : */
61 0 : export async function socialSignInWithProvisioning<T>(deps: {
62 : signIn: () => Promise<T>
63 : provisionCustomer: (credential: T) => Promise<unknown>
64 : }): Promise<T> {
65 0 : const credential = await deps.signIn()
66 0 : await deps.provisionCustomer(credential)
67 0 : return credential
68 : }
69 :
70 : export const CUSTOMER_PROVISIONING_PATH = '/customers'
71 :
72 : /**
73 : * The `POST /customers` request both hosts send. `signupSource` names the
74 : * host for the backend's attribution; the Turnstile token rides along only
75 : * on email sign-up, where the server validates it. Body and headers only: a
76 : * host that wants a bound passes its own `signal`.
77 : */
78 : export function customerProvisioningRequest(options: {
79 : authHeaders: Record<string, string>
80 : signupSource: string
81 : turnstileToken?: string
82 : signal?: AbortSignal
83 : }): RequestInit {
84 : const { authHeaders, signupSource, turnstileToken, signal } = options
85 : return {
86 : method: 'POST',
87 : headers: { ...authHeaders, 'Content-Type': 'application/json' },
88 : body: JSON.stringify({
89 : signup_source: signupSource,
90 : ...(turnstileToken ? { turnstile_token: turnstileToken } : {})
91 : }),
92 : ...(signal ? { signal } : {})
93 : }
94 : }
|