@elytracms/studio 0.0.54 → 0.0.56

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.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sign-in-view.js","sourceRoot":"","sources":["../../src/components/sign-in-view.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAEhC,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAA;AAC/D,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAA;AACpG,OAAO,EAAE,OAAO,EAAE,MAAM,2CAA2C,CAAA;AACnE,OAAO,EAAE,qBAAqB,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAA;AAE9F,OAAO,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAA;AAqCzF,6EAA6E;AAC7E,SAAS,iBAAiB;IACxB,MAAM,KAAK,GAAG,iBAAiB,EAAE,CAAA;IACjC,IAAI,KAAK,CAAC,IAAI,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAA;IACxC,MAAM,IAAI,GAAG,oBAAoB,EAAE,CAAA;IACnC,MAAM,OAAO,GAAG,CAAC,IAAI,IAAI,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,IAAI,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAA;IAC1F,OAAO,OAAO,EAAE,IAAI,IAAI,IAAI,CAAA;AAC9B,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,EAAE,SAAS,EAAE,WAAW,EAAmB;IACpE,MAAM,IAAI,GAAG,OAAO,EAAE,CAAA;IAC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,IAAI,CAAC,EAAE,CAEjB,CACR,CAAC,IACI,GACI,GACT,CACR,CAAA;AACH,CAAC","sourcesContent":["import { useState } from 'react'\nimport type { FormEvent, ReactNode } from 'react'\nimport { useNavigate, useSearch } from '@tanstack/react-router'\nimport { Button, Card, CardContent, Field, Heading, Input, Stack, Text } from '@elytracms/studio/ui'\nimport { useAuth } from '@elytracms/studio/components/auth-context'\nimport { LOCAL_DEV_CREDENTIALS, authMode, postSignInTarget } from '@elytracms/studio/lib/auth'\nimport type { SignInResult } from '@elytracms/studio/lib/auth'\nimport { getActiveProjectSlug, getWorkspaceState } from '@elytracms/studio/lib/workspace'\n\n/**\n * Engine-shipped `/sign-in` screen (EC-150; consolidated from the copy-pasted\n * consumer route by EC-375, EC-369 finding #8). The consumer route file keeps\n * only the route object — `createFileRoute('/sign-in')({ validateSearch,\n * component: SignInView })` — because TanStack Router requires the route object\n * to live in the consumer's route tree; the ~140-line body lives here so a\n * copy never drifts.\n *\n * Deterministic states: idle → submitting → (signed-in redirect | inline\n * structured failure). Invalid credentials are a rendered state, never a silent\n * failure or crash. In playground (in-memory) mode the seeded dev credentials\n * are listed so the whole flow works offline.\n *\n * Split into a `SignInView` container (owns the router + auth seams) and a\n * provider-free `SignInScreen` (pure local form state + injected callbacks) so\n * the screen static-renders in a node smoke test without the full provider tree.\n */\n\ntype FormPhase = 'idle' | 'submitting'\ntype AuthMode = 'signin' | 'signup'\n\n/** A seeded dev-credential entry surfaced in playground mode (fixtures, not secrets). */\ntype DevCredential = (typeof LOCAL_DEV_CREDENTIALS)[number]\n\nexport interface SignInViewProps {\n /**\n * Brand mark rendered above the heading. Defaults to the neutral studio\n * placeholder square — a future logo just passes a node here (e.g.\n * `component: () => <SignInView brandMark={<Logo />} />`), no engine change.\n */\n brandMark?: ReactNode\n /** Product name in the \"Sign in to …\" heading. Default: `'Elytra'`. */\n productName?: string\n}\n\n/** The project whose backend this sign-in authenticates against (EC-179). */\nfunction activeProjectName(): string | null {\n const state = getWorkspaceState()\n if (state.kind !== 'config') return null\n const slug = getActiveProjectSlug()\n const project = (slug && state.projects.find((p) => p.slug === slug)) || state.projects[0]\n return project?.name ?? null\n}\n\n/**\n * The `/sign-in` route component. Container: reads the router (post-sign-in\n * redirect target from the route search) + auth seams and hands the pure\n * presentational screen its data plus an `authenticate` callback.\n */\nexport function SignInView({ brandMark, productName }: SignInViewProps) {\n const auth = useAuth()\n const navigate = useNavigate()\n // Self-source the redirect (loose search — the consumer route's `validateSearch`\n // guarantees `{ redirect?: string }`), so the route file needs no wrapper.\n const search = useSearch({ strict: false }) as { redirect?: string }\n const redirect = search.redirect\n\n return (\n <SignInScreen\n projectName={activeProjectName()}\n showModeToggle={authMode() === 'convex'}\n devCredentials={authMode() === 'playground' ? LOCAL_DEV_CREDENTIALS : null}\n brandMark={brandMark}\n productName={productName}\n authenticate={(mode, email, password) =>\n mode === 'signup' ? auth.signUp(email, password) : auth.signIn(email, password)\n }\n onAuthenticated={() => navigate({ to: postSignInTarget(redirect) })}\n />\n )\n}\n\ninterface SignInScreenProps {\n projectName: string | null\n /** Whether to offer the sign-in ↔ create-account toggle (convex mode only). */\n showModeToggle: boolean\n /** Seeded dev accounts to list (playground mode), or null to hide the hint. */\n devCredentials: readonly DevCredential[] | null\n brandMark?: ReactNode\n productName?: string\n /** Attempt auth for the chosen mode. The container maps this to signIn/signUp. */\n authenticate: (mode: AuthMode, email: string, password: string) => Promise<SignInResult>\n /** Called once, after a successful authentication (the container navigates). */\n onAuthenticated: () => void | Promise<void>\n}\n\n/**\n * Provider-free presentational sign-in screen. Owns only local form state and\n * calls injected callbacks — no context hooks — so it static-renders in tests.\n */\nexport function SignInScreen({\n projectName,\n showModeToggle,\n devCredentials,\n brandMark,\n productName = 'Elytra',\n authenticate,\n onAuthenticated,\n}: SignInScreenProps) {\n const [email, setEmail] = useState('')\n const [password, setPassword] = useState('')\n const [phase, setPhase] = useState<FormPhase>('idle')\n const [mode, setMode] = useState<AuthMode>('signin')\n const [error, setError] = useState<string | null>(null)\n\n async function handleSubmit(event: FormEvent<HTMLFormElement>) {\n event.preventDefault()\n setPhase('submitting')\n setError(null)\n const result = await authenticate(mode, email, password)\n if (result.ok) {\n await onAuthenticated()\n return\n }\n // A fresh backend has no account yet: nudge sign-in → create, and vice versa.\n if (result.reason === 'account-exists') setMode('signin')\n setError(result.message)\n setPhase('idle')\n }\n\n const submitting = phase === 'submitting'\n const creating = mode === 'signup'\n\n return (\n <div className=\"grid min-h-dvh place-items-center bg-background p-6\">\n <Stack gap=\"4\" className=\"w-full max-w-sm\">\n <Stack gap=\"1\" align=\"center\">\n {brandMark ?? <span className=\"size-8 rounded-lg bg-primary shadow-sm\" aria-hidden />}\n <Heading size=\"h2\">\n {creating ? 'Create your account' : `Sign in to ${productName}`}\n </Heading>\n <Text size=\"sm\" tone=\"muted\">\n {projectName\n ? `${creating ? 'Register on' : 'Continue to'} ${projectName}.`\n : 'Use your studio account to continue.'}\n </Text>\n </Stack>\n\n <Card>\n <CardContent className=\"pt-5\">\n <form onSubmit={handleSubmit} noValidate>\n <Stack gap=\"4\">\n <Field label=\"Email\" htmlFor=\"sign-in-email\" required>\n <Input\n id=\"sign-in-email\"\n type=\"email\"\n autoComplete=\"email\"\n value={email}\n onChange={(e) => setEmail(e.target.value)}\n placeholder=\"you@example.com\"\n disabled={submitting}\n />\n </Field>\n <Field label=\"Password\" htmlFor=\"sign-in-password\" required error={error}>\n <Input\n id=\"sign-in-password\"\n type=\"password\"\n autoComplete={creating ? 'new-password' : 'current-password'}\n value={password}\n onChange={(e) => setPassword(e.target.value)}\n disabled={submitting}\n />\n </Field>\n <Button type=\"submit\" disabled={submitting} className=\"w-full\">\n {submitting\n ? creating\n ? 'Creating account…'\n : 'Signing in…'\n : creating\n ? 'Create account'\n : 'Sign in'}\n </Button>\n </Stack>\n </form>\n </CardContent>\n </Card>\n\n {showModeToggle ? (\n <Text size=\"sm\" tone=\"muted\" className=\"text-center\">\n {creating ? 'Already have an account on this backend? ' : 'First time on this backend? '}\n <button\n type=\"button\"\n className=\"font-medium text-primary underline-offset-2 hover:underline\"\n onClick={() => {\n setMode(creating ? 'signin' : 'signup')\n setError(null)\n }}\n >\n {creating ? 'Sign in' : 'Create an account'}\n </button>\n </Text>\n ) : null}\n\n {devCredentials ? <DevCredentialsHint entries={devCredentials} /> : null}\n </Stack>\n </div>\n )\n}\n\n/** Seeded dev accounts for the explicit playground mode — fixtures, not secrets. */\nfunction DevCredentialsHint({ entries }: { entries: readonly DevCredential[] }) {\n return (\n <Card className=\"border-dashed\">\n <CardContent className=\"pt-4\">\n <Stack gap=\"1\">\n <Text size=\"xs\" tone=\"muted\" className=\"font-medium\">\n Playground mode — seeded dev accounts\n </Text>\n {entries.map((entry) => (\n <Text key={entry.user.id} size=\"xs\" tone=\"muted\" className=\"font-mono\">\n {entry.user.email} / {entry.password}\n </Text>\n ))}\n </Stack>\n </CardContent>\n </Card>\n )\n}\n"]}
@@ -5,11 +5,15 @@ import type { SessionStore } from './local-auth-adapter.js';
5
5
  * Classify a thrown sign-in error as a DEPLOYMENT-setup failure rather than a
6
6
  * credential problem (EC-257). Matches the Convex Auth signing-path throws:
7
7
  * `requireEnv('JWT_PRIVATE_KEY'|'JWKS'|'CONVEX_SITE_URL')` (→ "Missing environment
8
- * variable …") and `importPKCS8` on a malformed key. Deliberately narrow — a genuine
9
- * wrong-password error carries none of these tokens — and checked BEFORE the credential
10
- * regex so it always wins. Note: a PRODUCTION deployment redacts non-`ConvexError`
11
- * messages to a generic "Server Error", which this can't match; there the honest
12
- * fallback is the neutral "unavailable" branch (never "password too short").
8
+ * variable …") and `importPKCS8` on a malformed key — plus the EC-363 server code
9
+ * `deployment-misconfigured` (`defineStudioAuth` wraps those library throws in a
10
+ * `ConvexError` whose `.data` survives prod redaction, so this classifies on
11
+ * PRODUCTION deployments too once their functions are redeployed). Deliberately
12
+ * narrow — a genuine wrong-password error carries none of these tokens — and checked
13
+ * BEFORE the credential regex so it always wins. On a prod deployment whose
14
+ * functions PREDATE EC-363 the plain-`Error` message is redacted to "Server Error",
15
+ * which this can't match; there the honest fallback is the neutral "unavailable"
16
+ * branch (never "password too short").
13
17
  */
14
18
  export declare function isDeploymentMisconfigured(message: string): boolean;
15
19
  /**
@@ -25,9 +29,11 @@ export declare function isDeploymentMisconfigured(message: string): boolean;
25
29
  * (`dist/esm/browser/http_client.js`, ~L388-396 + L459-461). The server keeps
26
30
  * `errorData` for a `ConvexError` in prod while redacting `errorMessage`. So the
27
31
  * only classifiable text that survives prod is the `.data` of a `ConvexError` —
28
- * e.g. our invite gate's `ConvexError(NOT_INVITED_REASON)`. On DEV deployments
29
- * nothing is redacted, so `.message` also carries the token (the regex ladder
30
- * still matches); non-Convex errors only have a `.message`.
32
+ * our invite gate's `ConvexError(NOT_INVITED_REASON)` and, since EC-363, the
33
+ * coded `ConvexError({ code })` that `defineStudioAuth` wraps around the
34
+ * library's credential/env throws. On DEV deployments nothing is redacted, so
35
+ * `.message` also carries the token (the regex ladder still matches);
36
+ * non-Convex errors only have a `.message`.
31
37
  *
32
38
  * Duck-types the `data` property rather than `instanceof ConvexError`: two
33
39
  * copies of the `convex` module can coexist under Vite (transformed vs
@@ -39,16 +45,25 @@ type SignInFailure = Extract<SignInResult, {
39
45
  ok: false;
40
46
  }>;
41
47
  /**
42
- * Pure sign-in/sign-up error classifier (EC-257 / EC-324 / EC-362). Runs the
43
- * regex ladder over {@link convexErrorClassifierText} output (data-first).
48
+ * Pure sign-in/sign-up error classifier (EC-257 / EC-324 / EC-362 / EC-363).
49
+ * Runs the regex ladder over {@link convexErrorClassifierText} output
50
+ * (data-first).
51
+ *
52
+ * EC-363: `defineStudioAuth` now wraps `@convex-dev/auth`'s plain-`Error`
53
+ * credential/env throws server-side in a `ConvexError` carrying a stable code
54
+ * in `.data` (`{"code":"invalid-credentials" | "too-many-attempts" |
55
+ * "account-exists" | "deployment-misconfigured"}`). The data-first text folds
56
+ * that JSON in, so the SAME ladder classifies the codes (they survive prod
57
+ * redaction) AND the raw dev-shape messages — a deployment whose functions
58
+ * predate EC-363 still classifies exactly as before (dev: full message; prod:
59
+ * the honest generic below).
44
60
  *
45
61
  * Order matters: a DEPLOYMENT-misconfiguration (EC-257) and an uninvited
46
62
  * sign-up (EC-324) are each checked BEFORE the credential regex so neither
47
63
  * degrades to the lying "password too short". The final fallback is HONEST
48
64
  * ("unexpected server error — check the deployment logs"), never "unavailable":
49
- * on a prod deployment the credential/`InvalidAccountId`/env errors thrown by
50
- * `@convex-dev/auth` are PLAIN `Error`s (not `ConvexError`s), so prod redacts
51
- * them to `"Server Error"` with no `.data` and they legitimately land here.
65
+ * unknown errors — and any plain `Error` a pre-EC-363 prod deployment redacts
66
+ * to `"Server Error"` with no `.data` — legitimately land here.
52
67
  */
53
68
  export declare function classifySignInError(flow: 'signIn' | 'signUp', text: string): SignInFailure;
54
69
  type ChangePasswordFailure = Extract<ChangePasswordResult, {
@@ -26,14 +26,18 @@ const TOKEN_STORAGE_PREFIX = 'elytra.convex-auth.jwt';
26
26
  * Classify a thrown sign-in error as a DEPLOYMENT-setup failure rather than a
27
27
  * credential problem (EC-257). Matches the Convex Auth signing-path throws:
28
28
  * `requireEnv('JWT_PRIVATE_KEY'|'JWKS'|'CONVEX_SITE_URL')` (→ "Missing environment
29
- * variable …") and `importPKCS8` on a malformed key. Deliberately narrow — a genuine
30
- * wrong-password error carries none of these tokens — and checked BEFORE the credential
31
- * regex so it always wins. Note: a PRODUCTION deployment redacts non-`ConvexError`
32
- * messages to a generic "Server Error", which this can't match; there the honest
33
- * fallback is the neutral "unavailable" branch (never "password too short").
29
+ * variable …") and `importPKCS8` on a malformed key — plus the EC-363 server code
30
+ * `deployment-misconfigured` (`defineStudioAuth` wraps those library throws in a
31
+ * `ConvexError` whose `.data` survives prod redaction, so this classifies on
32
+ * PRODUCTION deployments too once their functions are redeployed). Deliberately
33
+ * narrow — a genuine wrong-password error carries none of these tokens — and checked
34
+ * BEFORE the credential regex so it always wins. On a prod deployment whose
35
+ * functions PREDATE EC-363 the plain-`Error` message is redacted to "Server Error",
36
+ * which this can't match; there the honest fallback is the neutral "unavailable"
37
+ * branch (never "password too short").
34
38
  */
35
39
  export function isDeploymentMisconfigured(message) {
36
- return /JWT_PRIVATE_KEY|JWKS|CONVEX_SITE_URL|Missing environment variable|PKCS8|private key/i.test(message);
40
+ return /JWT_PRIVATE_KEY|JWKS|CONVEX_SITE_URL|Missing environment variable|PKCS8|private key|deployment-misconfigured/i.test(message);
37
41
  }
38
42
  /**
39
43
  * Reduce a thrown Convex error to the text used for classification (EC-362).
@@ -48,9 +52,11 @@ export function isDeploymentMisconfigured(message) {
48
52
  * (`dist/esm/browser/http_client.js`, ~L388-396 + L459-461). The server keeps
49
53
  * `errorData` for a `ConvexError` in prod while redacting `errorMessage`. So the
50
54
  * only classifiable text that survives prod is the `.data` of a `ConvexError` —
51
- * e.g. our invite gate's `ConvexError(NOT_INVITED_REASON)`. On DEV deployments
52
- * nothing is redacted, so `.message` also carries the token (the regex ladder
53
- * still matches); non-Convex errors only have a `.message`.
55
+ * our invite gate's `ConvexError(NOT_INVITED_REASON)` and, since EC-363, the
56
+ * coded `ConvexError({ code })` that `defineStudioAuth` wraps around the
57
+ * library's credential/env throws. On DEV deployments nothing is redacted, so
58
+ * `.message` also carries the token (the regex ladder still matches);
59
+ * non-Convex errors only have a `.message`.
54
60
  *
55
61
  * Duck-types the `data` property rather than `instanceof ConvexError`: two
56
62
  * copies of the `convex` module can coexist under Vite (transformed vs
@@ -82,16 +88,25 @@ export function convexErrorClassifierText(error) {
82
88
  return parts.join('\n');
83
89
  }
84
90
  /**
85
- * Pure sign-in/sign-up error classifier (EC-257 / EC-324 / EC-362). Runs the
86
- * regex ladder over {@link convexErrorClassifierText} output (data-first).
91
+ * Pure sign-in/sign-up error classifier (EC-257 / EC-324 / EC-362 / EC-363).
92
+ * Runs the regex ladder over {@link convexErrorClassifierText} output
93
+ * (data-first).
94
+ *
95
+ * EC-363: `defineStudioAuth` now wraps `@convex-dev/auth`'s plain-`Error`
96
+ * credential/env throws server-side in a `ConvexError` carrying a stable code
97
+ * in `.data` (`{"code":"invalid-credentials" | "too-many-attempts" |
98
+ * "account-exists" | "deployment-misconfigured"}`). The data-first text folds
99
+ * that JSON in, so the SAME ladder classifies the codes (they survive prod
100
+ * redaction) AND the raw dev-shape messages — a deployment whose functions
101
+ * predate EC-363 still classifies exactly as before (dev: full message; prod:
102
+ * the honest generic below).
87
103
  *
88
104
  * Order matters: a DEPLOYMENT-misconfiguration (EC-257) and an uninvited
89
105
  * sign-up (EC-324) are each checked BEFORE the credential regex so neither
90
106
  * degrades to the lying "password too short". The final fallback is HONEST
91
107
  * ("unexpected server error — check the deployment logs"), never "unavailable":
92
- * on a prod deployment the credential/`InvalidAccountId`/env errors thrown by
93
- * `@convex-dev/auth` are PLAIN `Error`s (not `ConvexError`s), so prod redacts
94
- * them to `"Server Error"` with no `.data` and they legitimately land here.
108
+ * unknown errors — and any plain `Error` a pre-EC-363 prod deployment redacts
109
+ * to `"Server Error"` with no `.data` — legitimately land here.
95
110
  */
96
111
  export function classifySignInError(flow, text) {
97
112
  // EC-257: a DEPLOYMENT-misconfiguration — its auth signing key is unset — must be
@@ -117,6 +132,20 @@ export function classifySignInError(flow, text) {
117
132
  message: 'This deployment is invite-only. Ask an admin to invite your email, then create your account.',
118
133
  };
119
134
  }
135
+ // EC-363: the sign-in rate limit. New deployments code it server-side
136
+ // (`{"code":"too-many-attempts"}` survives prod redaction); dev/old
137
+ // deployments surface the library's plain `TooManyFailedAttempts`. Checked
138
+ // BEFORE the credential regex; truthful wait-and-retry copy on the existing
139
+ // `unavailable` reason (a throttled deployment "can't sign you in right
140
+ // now" — the adapter contract gains no new reason for a transient state).
141
+ if (/too-many-attempts|TooManyFailedAttempts/i.test(text)) {
142
+ return {
143
+ ok: false,
144
+ reason: 'unavailable',
145
+ message: 'Too many failed sign-in attempts — wait a moment, then try again.',
146
+ };
147
+ }
148
+ // The EC-363 code `account-exists` matches via `exists`.
120
149
  if (flow === 'signUp' && /already|exists|InvalidAccountId/i.test(text)) {
121
150
  return {
122
151
  ok: false,
@@ -124,6 +153,7 @@ export function classifySignInError(flow, text) {
124
153
  message: 'An account with this email already exists — sign in instead.',
125
154
  };
126
155
  }
156
+ // The EC-363 code `invalid-credentials` matches via `credential`.
127
157
  if (/InvalidSecret|InvalidAccountId|credential|password/i.test(text)) {
128
158
  return {
129
159
  ok: false,
@@ -1 +1 @@
1
- {"version":3,"file":"convex-auth-adapter.js","sourceRoot":"","sources":["../../../src/lib/auth/convex-auth-adapter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAA;AAa/E;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,MAAM,oBAAoB,GAAG,wBAAwB,CAAA;AAErD;;;;;;;;;GASG;AACH,MAAM,UAAU,yBAAyB,CAAC,OAAe;IACvD,OAAO,sFAAsF,CAAC,IAAI,CAChG,OAAO,CACR,CAAA;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,yBAAyB,CAAC,KAAc;IACtD,MAAM,KAAK,GAAa,EAAE,CAAA;IAC1B,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,MAAM,IAAI,KAAK,EAAE,CAAC;QACnE,MAAM,IAAI,GAAI,KAA2B,CAAC,IAAI,CAAA;QAC9C,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC7B,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QAClB,CAAC;aAAM,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YAC/C,IAAI,CAAC;gBACH,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAA;YAClC,CAAC;YAAC,MAAM,CAAC;gBACP,qEAAqE;YACvE,CAA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E,EAAE,KAAK,EAAE,MAAM,EAAE,mBAAmB,EAAE,OAAO,EAAE,wBAAwB,EAAE,CAAA;QACtF,CAAC;QACD,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QAC1B,IAAI,CAAC;YACH,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,sBAAsB,CAAC,EAAE;gBAC1D,eAAe,EAAE,KAAK,CAAC,eAAe;gBACtC,WAAW,EAAE,KAAK,CAAC,WAAW;aAC/B,CAAC,CAAA;YACF,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,CAAA;QACrB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,6EAA6E;YAC7E,6EAA6E;YAC7E,mDAAmD;YACnD,OAAO,2BAA2B,CAAC,yBAAyB,CAAC,KAAK,CAAC,CAAC,CAAA;QACtE,CAAC;IACH,CAAC;IAEO,MAAM,CAAC,EAAY;QACzB,OAAO;YACL,EAAE,EAAE,EAAE,CAAC,EAAE;YACT,KAAK,EAAE,EAAE,CAAC,KAAK;YACf,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YACrC,0EAA0E;YAC1E,uEAAuE;YACvE,yBAAyB;YACzB,WAAW,EAAE,EAAE;SAChB,CAAA;IACH,CAAC;IAEO,UAAU;QAChB,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;QACpC,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAA;IACzB,CAAC;CACF","sourcesContent":["import type { ConvexHttpClient } from 'convex/browser'\nimport { actionRef, mutationRef, queryRef } from '../persistence/convex-client'\nimport type {\n AuthAdapter,\n AuthSession,\n ChangePasswordInput,\n ChangePasswordResult,\n SignInCredentials,\n SignInResult,\n UpdateProfileInput,\n UpdateProfileResult,\n} from './auth-adapter'\nimport type { SessionStore } from './local-auth-adapter'\n\n/**\n * Convex Auth implementation of the project-owned {@link AuthAdapter}\n * (EC-150). Decision record (also in `convex/auth.ts`): **Convex Auth** over\n * Clerk — self-contained on the Convex stack, sessions in our own tables, no\n * extra vendor; reachable only through this adapter so a swap stays local.\n *\n * Flow (server side lives in `convex/auth.ts` + `convex/users.ts`):\n * 1. `signIn` calls the `auth:signIn` action with the Password provider\n * (`flow: 'signIn'`); on success Convex returns a JWT + refresh token.\n * 2. The JWT is set on the *shared* Convex client (`client.setAuth`), so every\n * subsequent persistence query/mutation runs authenticated and the\n * server-side `requireRole` guards see the caller.\n * 3. `currentSession` restores the JWT from storage and asks `users:me`.\n * 4. `signOut` calls `auth:signOut` and clears the JWT.\n *\n * v1 scope note: the stored JWT is short-lived and this adapter does not yet\n * run the refresh-token loop (the official `@convex-dev/auth/react` client\n * does); an expired session simply resolves to \"signed out\" and the user signs\n * in again. Live-deployment hardening is EC-156. No live deployment exists in\n * this repo — this file is typecheck-verified and exercised against the local\n * adapter's shared interface.\n */\n\nconst TOKEN_STORAGE_PREFIX = 'elytra.convex-auth.jwt'\n\n/**\n * Classify a thrown sign-in error as a DEPLOYMENT-setup failure rather than a\n * credential problem (EC-257). Matches the Convex Auth signing-path throws:\n * `requireEnv('JWT_PRIVATE_KEY'|'JWKS'|'CONVEX_SITE_URL')` (→ \"Missing environment\n * variable …\") and `importPKCS8` on a malformed key. Deliberately narrow — a genuine\n * wrong-password error carries none of these tokens — and checked BEFORE the credential\n * regex so it always wins. Note: a PRODUCTION deployment redacts non-`ConvexError`\n * messages to a generic \"Server Error\", which this can't match; there the honest\n * fallback is the neutral \"unavailable\" branch (never \"password too short\").\n */\nexport function isDeploymentMisconfigured(message: string): boolean {\n return /JWT_PRIVATE_KEY|JWKS|CONVEX_SITE_URL|Missing environment variable|PKCS8|private key/i.test(\n message,\n )\n}\n\n/**\n * Reduce a thrown Convex error to the text used for classification (EC-362).\n *\n * **Read `.data` FIRST, then `.message`** — because a PRODUCTION Convex\n * deployment REDACTS a UDF's thrown-error `.message` to a generic\n * `\"[Request ID: …] Server Error\"`, but a `ConvexError`'s `.data` payload\n * survives to the client. Proven in the installed `convex@1.42.1` browser\n * client: on an `\"error\"` response `ConvexHttpClient.action` throws\n * `forwardErrorData(respJSON.errorData, new ConvexError(respJSON.errorMessage))`,\n * and `forwardErrorData` sets `error.data = jsonToConvex(errorData)`\n * (`dist/esm/browser/http_client.js`, ~L388-396 + L459-461). The server keeps\n * `errorData` for a `ConvexError` in prod while redacting `errorMessage`. So the\n * only classifiable text that survives prod is the `.data` of a `ConvexError` —\n * e.g. our invite gate's `ConvexError(NOT_INVITED_REASON)`. On DEV deployments\n * nothing is redacted, so `.message` also carries the token (the regex ladder\n * still matches); non-Convex errors only have a `.message`.\n *\n * Duck-types the `data` property rather than `instanceof ConvexError`: two\n * copies of the `convex` module can coexist under Vite (transformed vs\n * pre-bundled), so `instanceof` can spuriously fail (EC-357). `.data` may be a\n * string (our gates) or a structured object (future gates) — both are folded in.\n */\nexport function convexErrorClassifierText(error: unknown): string {\n const parts: string[] = []\n if (typeof error === 'object' && error !== null && 'data' in error) {\n const data = (error as { data: unknown }).data\n if (typeof data === 'string') {\n parts.push(data)\n } else if (data !== null && data !== undefined) {\n try {\n parts.push(JSON.stringify(data))\n } catch {\n // Non-serializable data (cycles) — ignore; fall back to the message.\n }\n }\n }\n if (error instanceof Error) parts.push(error.message)\n else if (typeof error === 'string') parts.push(error)\n if (parts.length === 0) parts.push(String(error))\n return parts.join('\\n')\n}\n\ntype SignInFailure = Extract<SignInResult, { ok: false }>\n\n/**\n * Pure sign-in/sign-up error classifier (EC-257 / EC-324 / EC-362). Runs the\n * regex ladder over {@link convexErrorClassifierText} output (data-first).\n *\n * Order matters: a DEPLOYMENT-misconfiguration (EC-257) and an uninvited\n * sign-up (EC-324) are each checked BEFORE the credential regex so neither\n * degrades to the lying \"password too short\". The final fallback is HONEST\n * (\"unexpected server error — check the deployment logs\"), never \"unavailable\":\n * on a prod deployment the credential/`InvalidAccountId`/env errors thrown by\n * `@convex-dev/auth` are PLAIN `Error`s (not `ConvexError`s), so prod redacts\n * them to `\"Server Error\"` with no `.data` and they legitimately land here.\n */\nexport function classifySignInError(flow: 'signIn' | 'signUp', text: string): SignInFailure {\n // EC-257: a DEPLOYMENT-misconfiguration — its auth signing key is unset — must be\n // classified FIRST so it never falls through to the lying \"password too short\".\n // Convex Auth signs the session JWT with the deployment's `JWT_PRIVATE_KEY`\n // (+ `JWKS`/`CONVEX_SITE_URL`); when any is unset (`npx @convex-dev/auth` never\n // run) the `requireEnv`/`importPKCS8` call throws AFTER the password is validated,\n // so EVERY sign-in fails regardless of the password.\n if (isDeploymentMisconfigured(text)) {\n return {\n ok: false,\n reason: 'deployment-misconfigured',\n message:\n 'This deployment can’t sign you in: its auth signing key is not set. Run `npx @convex-dev/auth` against this deployment, then retry. (The deployment-setup check reports the same.)',\n }\n }\n // EC-324: an uninvited sign-up is rejected server-side (`gateAccountCreation`/\n // `gateSessionCreation` throw `ConvexError(NOT_INVITED_REASON)`, whose `.data`\n // survives to the client even in production). Checked BEFORE the credential regex.\n if (/not-invited|invite-only|invite only/i.test(text)) {\n return {\n ok: false,\n reason: 'not-invited',\n message:\n 'This deployment is invite-only. Ask an admin to invite your email, then create your account.',\n }\n }\n if (flow === 'signUp' && /already|exists|InvalidAccountId/i.test(text)) {\n return {\n ok: false,\n reason: 'account-exists',\n message: 'An account with this email already exists — sign in instead.',\n }\n }\n if (/InvalidSecret|InvalidAccountId|credential|password/i.test(text)) {\n return {\n ok: false,\n reason: 'invalid-credentials',\n message:\n flow === 'signUp'\n ? 'That email or password can’t be used (password too short?).'\n : 'Email or password is incorrect.',\n }\n }\n const verb = flow === 'signUp' ? 'Sign-up' : 'Sign-in'\n return {\n ok: false,\n reason: 'unavailable',\n message: `${verb} failed (unexpected server error) — check the deployment logs.`,\n }\n}\n\ntype ChangePasswordFailure = Extract<ChangePasswordResult, { ok: false }>\n\n/**\n * Pure password-change error classifier (EC-240 H-tail / EC-362). Same\n * data-first text + honest fallback discipline as {@link classifySignInError};\n * `deployment-misconfigured` collapses onto the `unavailable` reason\n * (`ChangePasswordResult` has no dedicated reason for it) with a specific\n * remediation message.\n */\nexport function classifyChangePasswordError(text: string): ChangePasswordFailure {\n if (isDeploymentMisconfigured(text)) {\n return {\n ok: false,\n reason: 'unavailable',\n message:\n 'This deployment can’t change your password: its auth signing key is not set. Run `npx @convex-dev/auth`.',\n }\n }\n if (/InvalidSecret|InvalidAccountId|current password|incorrect/i.test(text)) {\n return {\n ok: false,\n reason: 'invalid-credentials',\n message: 'Your current password is incorrect.',\n }\n }\n if (/at least|too short|8 characters|password requirement|weak/i.test(text)) {\n return {\n ok: false,\n reason: 'weak-password',\n message: 'Choose a stronger password (at least 8 characters).',\n }\n }\n return {\n ok: false,\n reason: 'unavailable',\n message: 'Password change failed (unexpected server error) — check the deployment logs.',\n }\n}\n\n/**\n * Per-deployment token key (EC-179, vision AD-8). One project per backend, so a\n * session is scoped to its deployment: signing into Aurora's backend must not\n * carry a JWT to Northwind's. Keying the stored token by the deployment URL\n * makes each backend's session independent — switching to a project on a\n * not-yet-authenticated backend reads no token and the guard asks for sign-in.\n * (A future shared external IdP — WorkOS/Clerk — would issue one identity valid\n * on every deployment, collapsing this back to a single session.)\n */\nexport function tokenStorageKey(deploymentUrl?: string): string {\n return deploymentUrl ? `${TOKEN_STORAGE_PREFIX}::${deploymentUrl}` : TOKEN_STORAGE_PREFIX\n}\n\n/**\n * Best-effort \"is there a session for this deployment?\" — a synchronous token\n * presence check used to label per-backend projects in the switcher/launcher\n * (EC-179). It does not validate the token (an expired one still redirects to\n * sign-in on entry); it just answers \"has the user signed into this backend?\".\n */\nexport function hasStoredConvexSession(\n deploymentUrl: string,\n store: SessionStore = defaultStore(),\n): boolean {\n return store.getItem(tokenStorageKey(deploymentUrl)) !== null\n}\n\ninterface SignInActionResult {\n tokens?: { token: string; refreshToken: string } | null\n}\n\ninterface MeResult {\n id: string\n email: string\n name?: string\n}\n\nfunction defaultStore(): SessionStore {\n if (typeof window !== 'undefined' && window.sessionStorage) return window.sessionStorage\n const memory = new Map<string, string>()\n return {\n getItem: (key) => memory.get(key) ?? null,\n setItem: (key, value) => void memory.set(key, value),\n removeItem: (key) => void memory.delete(key),\n }\n}\n\nexport class ConvexAuthAdapter implements AuthAdapter {\n private readonly tokenKey: string\n\n constructor(\n private readonly client: ConvexHttpClient,\n private readonly store: SessionStore = defaultStore(),\n /** Deployment URL the token is scoped to (per-backend sessions, EC-179). */\n deploymentUrl?: string,\n ) {\n this.tokenKey = tokenStorageKey(deploymentUrl)\n }\n\n async currentSession(): Promise<AuthSession | null> {\n const token = this.store.getItem(this.tokenKey)\n if (!token) return null\n this.client.setAuth(token)\n try {\n const me = (await this.client.query(queryRef('users:me'), {})) as MeResult | null\n if (!me) {\n this.clearToken()\n return null\n }\n return { user: this.toUser(me) }\n } catch {\n // Expired/invalid JWT or unreachable backend → treat as signed out.\n this.clearToken()\n return null\n }\n }\n\n async signIn(credentials: SignInCredentials): Promise<SignInResult> {\n return this.authenticate('signIn', credentials)\n }\n\n async signUp(credentials: SignInCredentials): Promise<SignInResult> {\n return this.authenticate('signUp', credentials)\n }\n\n /**\n * Shared password flow (EC-150/EC-179). `signIn` and `signUp` differ only by\n * the Convex Auth `flow` param; both return a token + load `users:me`.\n */\n private async authenticate(\n flow: 'signIn' | 'signUp',\n credentials: SignInCredentials,\n ): Promise<SignInResult> {\n let result: SignInActionResult\n try {\n result = (await this.client.action(actionRef('auth:signIn'), {\n provider: 'password',\n params: {\n email: credentials.email.trim().toLowerCase(),\n password: credentials.password,\n flow,\n },\n })) as SignInActionResult\n } catch (error) {\n // Convex Auth surfaces credential/registration problems as thrown errors.\n // Classify from the DATA-FIRST text (a `ConvexError`'s `.data` survives prod\n // redaction, the redacted `.message` does not — EC-362), then apply the pure\n // regex ladder. See `convexErrorClassifierText` / `classifySignInError`.\n return classifySignInError(flow, convexErrorClassifierText(error))\n }\n if (!result.tokens) {\n return {\n ok: false,\n reason: 'invalid-credentials',\n message: 'Email or password is incorrect.',\n }\n }\n this.store.setItem(this.tokenKey, result.tokens.token)\n this.client.setAuth(result.tokens.token)\n const me = (await this.client.query(queryRef('users:me'), {})) as MeResult | null\n if (!me) {\n this.clearToken()\n return { ok: false, reason: 'unavailable', message: 'Sign-in is currently unavailable.' }\n }\n return { ok: true, session: { user: this.toUser(me) } }\n }\n\n async signOut(): Promise<void> {\n try {\n await this.client.action(actionRef('auth:signOut'), {})\n } catch {\n // Already signed out / unreachable — clearing local state is what matters.\n }\n this.clearToken()\n }\n\n /**\n * Self-service identity edit (EC-240 H-tail item 2). Patches the caller's OWN\n * auth `users` row (name / avatar) via the self-scoped `users:updateProfile`\n * mutation, then re-reads `users:me` so the returned session carries the new\n * name. Auth-owned fields never flow through the members-rail `users.save`.\n */\n async updateProfile(input: UpdateProfileInput): Promise<UpdateProfileResult> {\n const token = this.store.getItem(this.tokenKey)\n if (!token) {\n return { ok: false, reason: 'not-authenticated', message: 'You are not signed in.' }\n }\n this.client.setAuth(token)\n try {\n await this.client.mutation(mutationRef('users:updateProfile'), {\n ...(input.name !== undefined ? { name: input.name } : {}),\n ...(input.avatar !== undefined ? { avatar: input.avatar } : {}),\n })\n const me = (await this.client.query(queryRef('users:me'), {})) as MeResult | null\n if (!me) {\n return { ok: false, reason: 'unavailable', message: 'Profile update is currently unavailable.' }\n }\n return { ok: true, session: { user: this.toUser(me) } }\n } catch {\n return { ok: false, reason: 'unavailable', message: 'Profile update is currently unavailable.' }\n }\n }\n\n /**\n * Self-service password change (EC-240 H-tail item 3). Delegates entirely to\n * the provider via the self-scoped `users:changePassword` ACTION (which calls\n * Convex Auth's `retrieveAccount` to verify the current password, then\n * `modifyAccountCredentials` to set the new one). **Seam only:** the raw\n * password values are forwarded to the provider and NEVER logged or inspected\n * here.\n */\n async changePassword(input: ChangePasswordInput): Promise<ChangePasswordResult> {\n const token = this.store.getItem(this.tokenKey)\n if (!token) {\n return { ok: false, reason: 'not-authenticated', message: 'You are not signed in.' }\n }\n this.client.setAuth(token)\n try {\n await this.client.action(actionRef('users:changePassword'), {\n currentPassword: input.currentPassword,\n newPassword: input.newPassword,\n })\n return { ok: true }\n } catch (error) {\n // Data-first classification (EC-362): the change-password ACTION runs on the\n // same deployment, so a prod redaction erases the thrown `.message` the same\n // way — read a surviving `ConvexError.data` first.\n return classifyChangePasswordError(convexErrorClassifierText(error))\n }\n }\n\n private toUser(me: MeResult): AuthSession['user'] {\n return {\n id: me.id,\n email: me.email,\n ...(me.name ? { name: me.name } : {}),\n // No global flags: project creation is config/CLI-owned (AD-11), so there\n // is no in-studio `create-projects` capability. Per-project roles come\n // from `projectMembers`.\n globalFlags: [],\n }\n }\n\n private clearToken(): void {\n this.store.removeItem(this.tokenKey)\n this.client.clearAuth()\n }\n}\n"]}
1
+ {"version":3,"file":"convex-auth-adapter.js","sourceRoot":"","sources":["../../../src/lib/auth/convex-auth-adapter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAA;AAa/E;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,MAAM,oBAAoB,GAAG,wBAAwB,CAAA;AAErD;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,yBAAyB,CAAC,OAAe;IACvD,OAAO,+GAA+G,CAAC,IAAI,CACzH,OAAO,CACR,CAAA;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,yBAAyB,CAAC,KAAc;IACtD,MAAM,KAAK,GAAa,EAAE,CAAA;IAC1B,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,MAAM,IAAI,KAAK,EAAE,CAAC;QACnE,MAAM,IAAI,GAAI,KAA2B,CAAC,IAAI,CAAA;QAC9C,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CA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EAAE,EAAE,CAAC,KAAK;YACf,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YACrC,0EAA0E;YAC1E,uEAAuE;YACvE,yBAAyB;YACzB,WAAW,EAAE,EAAE;SAChB,CAAA;IACH,CAAC;IAEO,UAAU;QAChB,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;QACpC,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAA;IACzB,CAAC;CACF","sourcesContent":["import type { ConvexHttpClient } from 'convex/browser'\nimport { actionRef, mutationRef, queryRef } from '../persistence/convex-client'\nimport type {\n AuthAdapter,\n AuthSession,\n ChangePasswordInput,\n ChangePasswordResult,\n SignInCredentials,\n SignInResult,\n UpdateProfileInput,\n UpdateProfileResult,\n} from './auth-adapter'\nimport type { SessionStore } from './local-auth-adapter'\n\n/**\n * Convex Auth implementation of the project-owned {@link AuthAdapter}\n * (EC-150). Decision record (also in `convex/auth.ts`): **Convex Auth** over\n * Clerk — self-contained on the Convex stack, sessions in our own tables, no\n * extra vendor; reachable only through this adapter so a swap stays local.\n *\n * Flow (server side lives in `convex/auth.ts` + `convex/users.ts`):\n * 1. `signIn` calls the `auth:signIn` action with the Password provider\n * (`flow: 'signIn'`); on success Convex returns a JWT + refresh token.\n * 2. The JWT is set on the *shared* Convex client (`client.setAuth`), so every\n * subsequent persistence query/mutation runs authenticated and the\n * server-side `requireRole` guards see the caller.\n * 3. `currentSession` restores the JWT from storage and asks `users:me`.\n * 4. `signOut` calls `auth:signOut` and clears the JWT.\n *\n * v1 scope note: the stored JWT is short-lived and this adapter does not yet\n * run the refresh-token loop (the official `@convex-dev/auth/react` client\n * does); an expired session simply resolves to \"signed out\" and the user signs\n * in again. Live-deployment hardening is EC-156. No live deployment exists in\n * this repo — this file is typecheck-verified and exercised against the local\n * adapter's shared interface.\n */\n\nconst TOKEN_STORAGE_PREFIX = 'elytra.convex-auth.jwt'\n\n/**\n * Classify a thrown sign-in error as a DEPLOYMENT-setup failure rather than a\n * credential problem (EC-257). Matches the Convex Auth signing-path throws:\n * `requireEnv('JWT_PRIVATE_KEY'|'JWKS'|'CONVEX_SITE_URL')` (→ \"Missing environment\n * variable …\") and `importPKCS8` on a malformed key — plus the EC-363 server code\n * `deployment-misconfigured` (`defineStudioAuth` wraps those library throws in a\n * `ConvexError` whose `.data` survives prod redaction, so this classifies on\n * PRODUCTION deployments too once their functions are redeployed). Deliberately\n * narrow — a genuine wrong-password error carries none of these tokens — and checked\n * BEFORE the credential regex so it always wins. On a prod deployment whose\n * functions PREDATE EC-363 the plain-`Error` message is redacted to \"Server Error\",\n * which this can't match; there the honest fallback is the neutral \"unavailable\"\n * branch (never \"password too short\").\n */\nexport function isDeploymentMisconfigured(message: string): boolean {\n return /JWT_PRIVATE_KEY|JWKS|CONVEX_SITE_URL|Missing environment variable|PKCS8|private key|deployment-misconfigured/i.test(\n message,\n )\n}\n\n/**\n * Reduce a thrown Convex error to the text used for classification (EC-362).\n *\n * **Read `.data` FIRST, then `.message`** — because a PRODUCTION Convex\n * deployment REDACTS a UDF's thrown-error `.message` to a generic\n * `\"[Request ID: …] Server Error\"`, but a `ConvexError`'s `.data` payload\n * survives to the client. Proven in the installed `convex@1.42.1` browser\n * client: on an `\"error\"` response `ConvexHttpClient.action` throws\n * `forwardErrorData(respJSON.errorData, new ConvexError(respJSON.errorMessage))`,\n * and `forwardErrorData` sets `error.data = jsonToConvex(errorData)`\n * (`dist/esm/browser/http_client.js`, ~L388-396 + L459-461). The server keeps\n * `errorData` for a `ConvexError` in prod while redacting `errorMessage`. So the\n * only classifiable text that survives prod is the `.data` of a `ConvexError` —\n * our invite gate's `ConvexError(NOT_INVITED_REASON)` and, since EC-363, the\n * coded `ConvexError({ code })` that `defineStudioAuth` wraps around the\n * library's credential/env throws. On DEV deployments nothing is redacted, so\n * `.message` also carries the token (the regex ladder still matches);\n * non-Convex errors only have a `.message`.\n *\n * Duck-types the `data` property rather than `instanceof ConvexError`: two\n * copies of the `convex` module can coexist under Vite (transformed vs\n * pre-bundled), so `instanceof` can spuriously fail (EC-357). `.data` may be a\n * string (our gates) or a structured object (future gates) — both are folded in.\n */\nexport function convexErrorClassifierText(error: unknown): string {\n const parts: string[] = []\n if (typeof error === 'object' && error !== null && 'data' in error) {\n const data = (error as { data: unknown }).data\n if (typeof data === 'string') {\n parts.push(data)\n } else if (data !== null && data !== undefined) {\n try {\n parts.push(JSON.stringify(data))\n } catch {\n // Non-serializable data (cycles) — ignore; fall back to the message.\n }\n }\n }\n if (error instanceof Error) parts.push(error.message)\n else if (typeof error === 'string') parts.push(error)\n if (parts.length === 0) parts.push(String(error))\n return parts.join('\\n')\n}\n\ntype SignInFailure = Extract<SignInResult, { ok: false }>\n\n/**\n * Pure sign-in/sign-up error classifier (EC-257 / EC-324 / EC-362 / EC-363).\n * Runs the regex ladder over {@link convexErrorClassifierText} output\n * (data-first).\n *\n * EC-363: `defineStudioAuth` now wraps `@convex-dev/auth`'s plain-`Error`\n * credential/env throws server-side in a `ConvexError` carrying a stable code\n * in `.data` (`{\"code\":\"invalid-credentials\" | \"too-many-attempts\" |\n * \"account-exists\" | \"deployment-misconfigured\"}`). The data-first text folds\n * that JSON in, so the SAME ladder classifies the codes (they survive prod\n * redaction) AND the raw dev-shape messages — a deployment whose functions\n * predate EC-363 still classifies exactly as before (dev: full message; prod:\n * the honest generic below).\n *\n * Order matters: a DEPLOYMENT-misconfiguration (EC-257) and an uninvited\n * sign-up (EC-324) are each checked BEFORE the credential regex so neither\n * degrades to the lying \"password too short\". The final fallback is HONEST\n * (\"unexpected server error — check the deployment logs\"), never \"unavailable\":\n * unknown errors — and any plain `Error` a pre-EC-363 prod deployment redacts\n * to `\"Server Error\"` with no `.data` — legitimately land here.\n */\nexport function classifySignInError(flow: 'signIn' | 'signUp', text: string): SignInFailure {\n // EC-257: a DEPLOYMENT-misconfiguration — its auth signing key is unset — must be\n // classified FIRST so it never falls through to the lying \"password too short\".\n // Convex Auth signs the session JWT with the deployment's `JWT_PRIVATE_KEY`\n // (+ `JWKS`/`CONVEX_SITE_URL`); when any is unset (`npx @convex-dev/auth` never\n // run) the `requireEnv`/`importPKCS8` call throws AFTER the password is validated,\n // so EVERY sign-in fails regardless of the password.\n if (isDeploymentMisconfigured(text)) {\n return {\n ok: false,\n reason: 'deployment-misconfigured',\n message:\n 'This deployment can’t sign you in: its auth signing key is not set. Run `npx @convex-dev/auth` against this deployment, then retry. (The deployment-setup check reports the same.)',\n }\n }\n // EC-324: an uninvited sign-up is rejected server-side (`gateAccountCreation`/\n // `gateSessionCreation` throw `ConvexError(NOT_INVITED_REASON)`, whose `.data`\n // survives to the client even in production). Checked BEFORE the credential regex.\n if (/not-invited|invite-only|invite only/i.test(text)) {\n return {\n ok: false,\n reason: 'not-invited',\n message:\n 'This deployment is invite-only. Ask an admin to invite your email, then create your account.',\n }\n }\n // EC-363: the sign-in rate limit. New deployments code it server-side\n // (`{\"code\":\"too-many-attempts\"}` survives prod redaction); dev/old\n // deployments surface the library's plain `TooManyFailedAttempts`. Checked\n // BEFORE the credential regex; truthful wait-and-retry copy on the existing\n // `unavailable` reason (a throttled deployment \"can't sign you in right\n // now\" — the adapter contract gains no new reason for a transient state).\n if (/too-many-attempts|TooManyFailedAttempts/i.test(text)) {\n return {\n ok: false,\n reason: 'unavailable',\n message: 'Too many failed sign-in attempts — wait a moment, then try again.',\n }\n }\n // The EC-363 code `account-exists` matches via `exists`.\n if (flow === 'signUp' && /already|exists|InvalidAccountId/i.test(text)) {\n return {\n ok: false,\n reason: 'account-exists',\n message: 'An account with this email already exists — sign in instead.',\n }\n }\n // The EC-363 code `invalid-credentials` matches via `credential`.\n if (/InvalidSecret|InvalidAccountId|credential|password/i.test(text)) {\n return {\n ok: false,\n reason: 'invalid-credentials',\n message:\n flow === 'signUp'\n ? 'That email or password can’t be used (password too short?).'\n : 'Email or password is incorrect.',\n }\n }\n const verb = flow === 'signUp' ? 'Sign-up' : 'Sign-in'\n return {\n ok: false,\n reason: 'unavailable',\n message: `${verb} failed (unexpected server error) — check the deployment logs.`,\n }\n}\n\ntype ChangePasswordFailure = Extract<ChangePasswordResult, { ok: false }>\n\n/**\n * Pure password-change error classifier (EC-240 H-tail / EC-362). Same\n * data-first text + honest fallback discipline as {@link classifySignInError};\n * `deployment-misconfigured` collapses onto the `unavailable` reason\n * (`ChangePasswordResult` has no dedicated reason for it) with a specific\n * remediation message.\n */\nexport function classifyChangePasswordError(text: string): ChangePasswordFailure {\n if (isDeploymentMisconfigured(text)) {\n return {\n ok: false,\n reason: 'unavailable',\n message:\n 'This deployment can’t change your password: its auth signing key is not set. Run `npx @convex-dev/auth`.',\n }\n }\n if (/InvalidSecret|InvalidAccountId|current password|incorrect/i.test(text)) {\n return {\n ok: false,\n reason: 'invalid-credentials',\n message: 'Your current password is incorrect.',\n }\n }\n if (/at least|too short|8 characters|password requirement|weak/i.test(text)) {\n return {\n ok: false,\n reason: 'weak-password',\n message: 'Choose a stronger password (at least 8 characters).',\n }\n }\n return {\n ok: false,\n reason: 'unavailable',\n message: 'Password change failed (unexpected server error) — check the deployment logs.',\n }\n}\n\n/**\n * Per-deployment token key (EC-179, vision AD-8). One project per backend, so a\n * session is scoped to its deployment: signing into Aurora's backend must not\n * carry a JWT to Northwind's. Keying the stored token by the deployment URL\n * makes each backend's session independent — switching to a project on a\n * not-yet-authenticated backend reads no token and the guard asks for sign-in.\n * (A future shared external IdP — WorkOS/Clerk — would issue one identity valid\n * on every deployment, collapsing this back to a single session.)\n */\nexport function tokenStorageKey(deploymentUrl?: string): string {\n return deploymentUrl ? `${TOKEN_STORAGE_PREFIX}::${deploymentUrl}` : TOKEN_STORAGE_PREFIX\n}\n\n/**\n * Best-effort \"is there a session for this deployment?\" — a synchronous token\n * presence check used to label per-backend projects in the switcher/launcher\n * (EC-179). It does not validate the token (an expired one still redirects to\n * sign-in on entry); it just answers \"has the user signed into this backend?\".\n */\nexport function hasStoredConvexSession(\n deploymentUrl: string,\n store: SessionStore = defaultStore(),\n): boolean {\n return store.getItem(tokenStorageKey(deploymentUrl)) !== null\n}\n\ninterface SignInActionResult {\n tokens?: { token: string; refreshToken: string } | null\n}\n\ninterface MeResult {\n id: string\n email: string\n name?: string\n}\n\nfunction defaultStore(): SessionStore {\n if (typeof window !== 'undefined' && window.sessionStorage) return window.sessionStorage\n const memory = new Map<string, string>()\n return {\n getItem: (key) => memory.get(key) ?? null,\n setItem: (key, value) => void memory.set(key, value),\n removeItem: (key) => void memory.delete(key),\n }\n}\n\nexport class ConvexAuthAdapter implements AuthAdapter {\n private readonly tokenKey: string\n\n constructor(\n private readonly client: ConvexHttpClient,\n private readonly store: SessionStore = defaultStore(),\n /** Deployment URL the token is scoped to (per-backend sessions, EC-179). */\n deploymentUrl?: string,\n ) {\n this.tokenKey = tokenStorageKey(deploymentUrl)\n }\n\n async currentSession(): Promise<AuthSession | null> {\n const token = this.store.getItem(this.tokenKey)\n if (!token) return null\n this.client.setAuth(token)\n try {\n const me = (await this.client.query(queryRef('users:me'), {})) as MeResult | null\n if (!me) {\n this.clearToken()\n return null\n }\n return { user: this.toUser(me) }\n } catch {\n // Expired/invalid JWT or unreachable backend → treat as signed out.\n this.clearToken()\n return null\n }\n }\n\n async signIn(credentials: SignInCredentials): Promise<SignInResult> {\n return this.authenticate('signIn', credentials)\n }\n\n async signUp(credentials: SignInCredentials): Promise<SignInResult> {\n return this.authenticate('signUp', credentials)\n }\n\n /**\n * Shared password flow (EC-150/EC-179). `signIn` and `signUp` differ only by\n * the Convex Auth `flow` param; both return a token + load `users:me`.\n */\n private async authenticate(\n flow: 'signIn' | 'signUp',\n credentials: SignInCredentials,\n ): Promise<SignInResult> {\n let result: SignInActionResult\n try {\n result = (await this.client.action(actionRef('auth:signIn'), {\n provider: 'password',\n params: {\n email: credentials.email.trim().toLowerCase(),\n password: credentials.password,\n flow,\n },\n })) as SignInActionResult\n } catch (error) {\n // Convex Auth surfaces credential/registration problems as thrown errors.\n // Classify from the DATA-FIRST text (a `ConvexError`'s `.data` survives prod\n // redaction, the redacted `.message` does not — EC-362), then apply the pure\n // regex ladder. See `convexErrorClassifierText` / `classifySignInError`.\n return classifySignInError(flow, convexErrorClassifierText(error))\n }\n if (!result.tokens) {\n return {\n ok: false,\n reason: 'invalid-credentials',\n message: 'Email or password is incorrect.',\n }\n }\n this.store.setItem(this.tokenKey, result.tokens.token)\n this.client.setAuth(result.tokens.token)\n const me = (await this.client.query(queryRef('users:me'), {})) as MeResult | null\n if (!me) {\n this.clearToken()\n return { ok: false, reason: 'unavailable', message: 'Sign-in is currently unavailable.' }\n }\n return { ok: true, session: { user: this.toUser(me) } }\n }\n\n async signOut(): Promise<void> {\n try {\n await this.client.action(actionRef('auth:signOut'), {})\n } catch {\n // Already signed out / unreachable — clearing local state is what matters.\n }\n this.clearToken()\n }\n\n /**\n * Self-service identity edit (EC-240 H-tail item 2). Patches the caller's OWN\n * auth `users` row (name / avatar) via the self-scoped `users:updateProfile`\n * mutation, then re-reads `users:me` so the returned session carries the new\n * name. Auth-owned fields never flow through the members-rail `users.save`.\n */\n async updateProfile(input: UpdateProfileInput): Promise<UpdateProfileResult> {\n const token = this.store.getItem(this.tokenKey)\n if (!token) {\n return { ok: false, reason: 'not-authenticated', message: 'You are not signed in.' }\n }\n this.client.setAuth(token)\n try {\n await this.client.mutation(mutationRef('users:updateProfile'), {\n ...(input.name !== undefined ? { name: input.name } : {}),\n ...(input.avatar !== undefined ? { avatar: input.avatar } : {}),\n })\n const me = (await this.client.query(queryRef('users:me'), {})) as MeResult | null\n if (!me) {\n return { ok: false, reason: 'unavailable', message: 'Profile update is currently unavailable.' }\n }\n return { ok: true, session: { user: this.toUser(me) } }\n } catch {\n return { ok: false, reason: 'unavailable', message: 'Profile update is currently unavailable.' }\n }\n }\n\n /**\n * Self-service password change (EC-240 H-tail item 3). Delegates entirely to\n * the provider via the self-scoped `users:changePassword` ACTION (which calls\n * Convex Auth's `retrieveAccount` to verify the current password, then\n * `modifyAccountCredentials` to set the new one). **Seam only:** the raw\n * password values are forwarded to the provider and NEVER logged or inspected\n * here.\n */\n async changePassword(input: ChangePasswordInput): Promise<ChangePasswordResult> {\n const token = this.store.getItem(this.tokenKey)\n if (!token) {\n return { ok: false, reason: 'not-authenticated', message: 'You are not signed in.' }\n }\n this.client.setAuth(token)\n try {\n await this.client.action(actionRef('users:changePassword'), {\n currentPassword: input.currentPassword,\n newPassword: input.newPassword,\n })\n return { ok: true }\n } catch (error) {\n // Data-first classification (EC-362): the change-password ACTION runs on the\n // same deployment, so a prod redaction erases the thrown `.message` the same\n // way — read a surviving `ConvexError.data` first.\n return classifyChangePasswordError(convexErrorClassifierText(error))\n }\n }\n\n private toUser(me: MeResult): AuthSession['user'] {\n return {\n id: me.id,\n email: me.email,\n ...(me.name ? { name: me.name } : {}),\n // No global flags: project creation is config/CLI-owned (AD-11), so there\n // is no in-studio `create-projects` capability. Per-project roles come\n // from `projectMembers`.\n globalFlags: [],\n }\n }\n\n private clearToken(): void {\n this.store.removeItem(this.tokenKey)\n this.client.clearAuth()\n }\n}\n"]}
@@ -35,8 +35,60 @@ export type EnvRecord = Record<string, string | undefined>;
35
35
  * use this instead — keeping results independent of any developer `.env.local`.
36
36
  */
37
37
  export declare function setStudioEnvOverrideForTests(env: EnvRecord | null): void;
38
- /** Resolve the configured studio env (Vite client env wins over process.env). */
38
+ /**
39
+ * Install the CONSUMER-captured env as the studio's env source (EC-357). The root
40
+ * cause this fixes: the engine used to read `import.meta.env.VITE_*` inside
41
+ * `@elytracms` package code, which Vite dep-optimizes in a real consumer — on
42
+ * Vite ≥8 those values are simply absent at runtime in the optimized chunk (the
43
+ * backend URL / mode / dev-login silently vanish and every sign-in fails), and on
44
+ * Vite 7 they froze at optimize time (stale until `node_modules/.vite` is wiped).
45
+ *
46
+ * The consumer's own entry module is code Vite ALWAYS transforms live, so the env
47
+ * is captured there and handed down: `createStudio(config, studioConfig, { env:
48
+ * import.meta.env })`. When a provided env exists it REPLACES the engine-visible
49
+ * Vite layer entirely (never merged with it — a stale optimizer snapshot must not
50
+ * shadow a removed key); `process.env` stays underneath as the SSR/tooling base.
51
+ *
52
+ * `DEV` is captured alongside (it gates dev auto-login): in a production consumer
53
+ * build Vite statically inlines `import.meta.env` with `DEV: false`, so the
54
+ * dev-login gate keeps failing closed exactly as before. Non-string values other
55
+ * than `DEV` are dropped — the studio env surface is a string record.
56
+ *
57
+ * Passing `null`/`undefined` is a no-op (a consumer entry that predates the option
58
+ * keeps today's engine-visible fallback behavior, plus a loud runtime warning when
59
+ * that fallback is provably broken — see {@link maybeWarnEngineEnvUnreachable}).
60
+ */
61
+ export declare function provideStudioEnv(env: Record<string, unknown> | null | undefined): void;
62
+ /** Test seam: clear the mount-provided env. Never used by app code. */
63
+ export declare function resetProvidedStudioEnvForTests(): void;
64
+ /**
65
+ * Resolve the configured studio env. Precedence: the test override, else the
66
+ * mount-provided env (over `process.env`), else the engine-visible Vite env
67
+ * (over `process.env`).
68
+ */
39
69
  export declare function studioEnv(): EnvRecord;
70
+ /** The env keys whose loss/staleness breaks boot or sign-in. */
71
+ export declare const STUDIO_ENV_KEYS: readonly string[];
72
+ export type StudioEnvAnomaly =
73
+ /** No mount-provided env AND the engine module has no `import.meta.env` at all. */
74
+ {
75
+ kind: 'engine-env-unreachable';
76
+ }
77
+ /** The engine-visible env disagrees with the mount-provided env (stale optimizer cache). */
78
+ | {
79
+ kind: 'engine-env-stale';
80
+ keys: string[];
81
+ };
82
+ /**
83
+ * Pure anomaly detection over the two env views (unit-testable core of the boot
84
+ * warnings). `engineVisible === null` means the engine module has no
85
+ * `import.meta.env` — expected under dep optimization WITH a provided env (silent),
86
+ * fatal without one (the values can never arrive). A stale mismatch fires only for
87
+ * keys the ENGINE sees with a different value than the mount provided (the Vite 7
88
+ * freeze); keys the engine simply lacks (Vite 8) are not anomalies — the provided
89
+ * env is authoritative and in effect.
90
+ */
91
+ export declare function detectStudioEnvAnomaly(provided: EnvRecord | null, engineVisible: EnvRecord | null, inBrowser: boolean): StudioEnvAnomaly | null;
40
92
  /**
41
93
  * Pure mode decision — testable with a plain env record. The app calls it with
42
94
  * {@link studioEnv}; `CONVEX_URL` (Node-side alias) is honored for SSR/tools.
@@ -15,10 +15,22 @@
15
15
  export const MODE_ENV_VAR = 'VITE_ELYTRA_MODE';
16
16
  export const PLAYGROUND_MODE = 'playground';
17
17
  export const CONVEX_URL_ENV_VAR = 'VITE_CONVEX_URL';
18
- function viteEnv() {
18
+ /**
19
+ * The ENGINE-visible `import.meta.env`, or `null` when the module has none at all.
20
+ * `null` is a meaningful signal (EC-357): every module Vite transforms live gets an
21
+ * `import.meta.env`, so its complete absence in a browser means this engine module
22
+ * was served from Vite's dep-optimizer cache — where Vite ≥8 no longer inlines
23
+ * custom `VITE_*` values (and Vite 7 froze them at optimize time). Engine code must
24
+ * therefore never trust this view when a mount-provided env exists — see
25
+ * {@link provideStudioEnv}.
26
+ */
27
+ function engineVisibleEnv() {
19
28
  if (typeof import.meta === 'undefined')
20
- return {};
21
- return import.meta.env ?? {};
29
+ return null;
30
+ return import.meta.env ?? null;
31
+ }
32
+ function viteEnv() {
33
+ return engineVisibleEnv() ?? {};
22
34
  }
23
35
  function nodeEnv() {
24
36
  return typeof process !== 'undefined' ? (process.env ?? {}) : {};
@@ -37,9 +49,119 @@ let envOverride = null;
37
49
  export function setStudioEnvOverrideForTests(env) {
38
50
  envOverride = env;
39
51
  }
40
- /** Resolve the configured studio env (Vite client env wins over process.env). */
52
+ let providedEnv = null;
53
+ /**
54
+ * Install the CONSUMER-captured env as the studio's env source (EC-357). The root
55
+ * cause this fixes: the engine used to read `import.meta.env.VITE_*` inside
56
+ * `@elytracms` package code, which Vite dep-optimizes in a real consumer — on
57
+ * Vite ≥8 those values are simply absent at runtime in the optimized chunk (the
58
+ * backend URL / mode / dev-login silently vanish and every sign-in fails), and on
59
+ * Vite 7 they froze at optimize time (stale until `node_modules/.vite` is wiped).
60
+ *
61
+ * The consumer's own entry module is code Vite ALWAYS transforms live, so the env
62
+ * is captured there and handed down: `createStudio(config, studioConfig, { env:
63
+ * import.meta.env })`. When a provided env exists it REPLACES the engine-visible
64
+ * Vite layer entirely (never merged with it — a stale optimizer snapshot must not
65
+ * shadow a removed key); `process.env` stays underneath as the SSR/tooling base.
66
+ *
67
+ * `DEV` is captured alongside (it gates dev auto-login): in a production consumer
68
+ * build Vite statically inlines `import.meta.env` with `DEV: false`, so the
69
+ * dev-login gate keeps failing closed exactly as before. Non-string values other
70
+ * than `DEV` are dropped — the studio env surface is a string record.
71
+ *
72
+ * Passing `null`/`undefined` is a no-op (a consumer entry that predates the option
73
+ * keeps today's engine-visible fallback behavior, plus a loud runtime warning when
74
+ * that fallback is provably broken — see {@link maybeWarnEngineEnvUnreachable}).
75
+ */
76
+ export function provideStudioEnv(env) {
77
+ if (env === null || env === undefined)
78
+ return;
79
+ const record = {};
80
+ for (const [key, value] of Object.entries(env)) {
81
+ if (typeof value === 'string')
82
+ record[key] = value;
83
+ }
84
+ providedEnv = {
85
+ record,
86
+ dev: env.DEV === true ? true : env.DEV === false ? false : undefined,
87
+ };
88
+ maybeWarnEngineEnvStale();
89
+ }
90
+ /** Test seam: clear the mount-provided env. Never used by app code. */
91
+ export function resetProvidedStudioEnvForTests() {
92
+ providedEnv = null;
93
+ warnedStale = false;
94
+ warnedUnreachable = false;
95
+ }
96
+ /**
97
+ * Resolve the configured studio env. Precedence: the test override, else the
98
+ * mount-provided env (over `process.env`), else the engine-visible Vite env
99
+ * (over `process.env`).
100
+ */
41
101
  export function studioEnv() {
42
- return envOverride ?? { ...nodeEnv(), ...viteEnv() };
102
+ if (envOverride)
103
+ return envOverride;
104
+ if (providedEnv)
105
+ return { ...nodeEnv(), ...providedEnv.record };
106
+ maybeWarnEngineEnvUnreachable();
107
+ return { ...nodeEnv(), ...viteEnv() };
108
+ }
109
+ // --- Boot-time env self-check (EC-357, Oli: "easier to catch") ---------------
110
+ /** The env keys whose loss/staleness breaks boot or sign-in. */
111
+ export const STUDIO_ENV_KEYS = [
112
+ 'VITE_ELYTRA_MODE',
113
+ 'VITE_CONVEX_URL',
114
+ 'VITE_ELYTRA_DEV_LOGIN',
115
+ ];
116
+ /**
117
+ * Pure anomaly detection over the two env views (unit-testable core of the boot
118
+ * warnings). `engineVisible === null` means the engine module has no
119
+ * `import.meta.env` — expected under dep optimization WITH a provided env (silent),
120
+ * fatal without one (the values can never arrive). A stale mismatch fires only for
121
+ * keys the ENGINE sees with a different value than the mount provided (the Vite 7
122
+ * freeze); keys the engine simply lacks (Vite 8) are not anomalies — the provided
123
+ * env is authoritative and in effect.
124
+ */
125
+ export function detectStudioEnvAnomaly(provided, engineVisible, inBrowser) {
126
+ if (!inBrowser)
127
+ return null;
128
+ if (!provided)
129
+ return engineVisible === null ? { kind: 'engine-env-unreachable' } : null;
130
+ if (engineVisible === null)
131
+ return null;
132
+ const stale = STUDIO_ENV_KEYS.filter((key) => {
133
+ const engineValue = readEnv(engineVisible, key);
134
+ return engineValue !== undefined && engineValue !== readEnv(provided, key);
135
+ });
136
+ return stale.length > 0 ? { kind: 'engine-env-stale', keys: stale } : null;
137
+ }
138
+ function inBrowser() {
139
+ return typeof window !== 'undefined' && typeof document !== 'undefined';
140
+ }
141
+ let warnedUnreachable = false;
142
+ function maybeWarnEngineEnvUnreachable() {
143
+ if (warnedUnreachable)
144
+ return;
145
+ const anomaly = detectStudioEnvAnomaly(null, engineVisibleEnv(), inBrowser());
146
+ if (anomaly?.kind !== 'engine-env-unreachable')
147
+ return;
148
+ warnedUnreachable = true;
149
+ console.warn('[elytra] The engine cannot see import.meta.env — this @elytracms module was dep-optimized by Vite, ' +
150
+ 'so VITE_CONVEX_URL / VITE_ELYTRA_MODE / VITE_ELYTRA_DEV_LOGIN cannot reach it at runtime. ' +
151
+ 'Fix: pass your studio entry’s env to the mount — createStudio(config, studioConfig, { env: import.meta.env }) — ' +
152
+ "or add optimizeDeps.exclude: ['@elytracms/studio', '@elytracms/core'] to the studio vite.config.ts.");
153
+ }
154
+ let warnedStale = false;
155
+ function maybeWarnEngineEnvStale() {
156
+ if (warnedStale || !providedEnv)
157
+ return;
158
+ const anomaly = detectStudioEnvAnomaly(providedEnv.record, engineVisibleEnv(), inBrowser());
159
+ if (anomaly?.kind !== 'engine-env-stale')
160
+ return;
161
+ warnedStale = true;
162
+ console.warn(`[elytra] import.meta.env inside the engine disagrees with the studio mount's env for: ${anomaly.keys.join(', ')}. ` +
163
+ "The engine copy is a stale Vite dep-optimizer snapshot; the mount-provided values are in effect. " +
164
+ 'Delete node_modules/.vite to drop the stale copy.');
43
165
  }
44
166
  /**
45
167
  * Pure mode decision — testable with a plain env record. The app calls it with
@@ -62,7 +184,11 @@ export function resolveBackendConfig(env = studioEnv()) {
62
184
  return {
63
185
  kind: 'misconfigured',
64
186
  reason: 'missing-convex-url',
65
- detail: `No persistence backend is configured. Set ${CONVEX_URL_ENV_VAR} to your Convex deployment URL (see docs/backend-setup.md), or opt into the ephemeral local playground with ${MODE_ENV_VAR}=${PLAYGROUND_MODE}.`,
187
+ // EC-357: name BOTH causes — the var may be genuinely unset, or set but never
188
+ // reaching the client at runtime (Vite dep-optimized engine code; see
189
+ // `provideStudioEnv`). The paired fix text (deployment-ready `FIX.backendUrl`)
190
+ // carries the full remedy for the second cause.
191
+ detail: `No persistence backend is configured — ${CONVEX_URL_ENV_VAR} is unset or is not reaching the client at runtime. Set it to your Convex deployment URL (see docs/backend-setup.md), or opt into the ephemeral local playground with ${MODE_ENV_VAR}=${PLAYGROUND_MODE}.`,
66
192
  };
67
193
  }
68
194
  /** True when the studio runs in the explicit, labeled playground mode. */
@@ -75,12 +201,17 @@ export function isPlaygroundMode() {
75
201
  /** `email:password` for unattended dev sign-in (gitignored `.env.local`). */
76
202
  export const DEV_LOGIN_ENV_VAR = 'VITE_ELYTRA_DEV_LOGIN';
77
203
  /**
78
- * Is this a Vite *dev* build? `import.meta.env.DEV` is statically `true` under
79
- * `vite dev` and statically `false` in any production build — so gating dev
80
- * auto-login on it means the whole feature **dead-code-eliminates** out of a
81
- * production bundle, even if `VITE_ELYTRA_DEV_LOGIN` were somehow present.
204
+ * Is this a Vite *dev* build? The mount-provided env's captured `DEV` flag wins
205
+ * (EC-357 — inside dep-optimized engine code the engine-visible flag is absent on
206
+ * Vite ≥8 and frozen on Vite 7); the consumer's `import.meta.env.DEV` is statically
207
+ * `true` under `vite dev` and statically `false` in any production build, so the
208
+ * dev auto-login gate still fails closed in production. Without a provided env the
209
+ * engine-visible static flag applies as before (where it also dead-code-eliminates
210
+ * the feature out of a directly-bundled production build).
82
211
  */
83
212
  function isViteDevBuild() {
213
+ if (providedEnv?.dev !== undefined)
214
+ return providedEnv.dev;
84
215
  if (typeof import.meta === 'undefined')
85
216
  return false;
86
217
  return import.meta.env?.DEV === true;