-
Notifications
You must be signed in to change notification settings - Fork 0
feat(dev): ENABLE_DEV_AUTH — run the stack without Clerk for local dev #145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: staging
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,63 @@ | ||||||||||||
| import { | ||||||||||||
| useAuth as clerkUseAuth, | ||||||||||||
| useClerk as clerkUseClerk, | ||||||||||||
| useReverification as clerkUseReverification, | ||||||||||||
| useUser as clerkUseUser, | ||||||||||||
| } from "@clerk/tanstack-react-start"; | ||||||||||||
| import { env } from "../env"; | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Single switch for the loginless local-dev mode. When `VITE_ENABLE_DEV_AUTH` is | ||||||||||||
| * "true", the app skips Clerk entirely and runs as a fixed dev user — pair it | ||||||||||||
| * with `ENABLE_DEV_AUTH` on the Convex deployment and the FastAPI gateway. | ||||||||||||
| * | ||||||||||||
| * Off by default, and `DEV_AUTH` is a build-time constant, so the exported hooks | ||||||||||||
| * below resolve to the REAL Clerk hooks in every normal build — this module is a | ||||||||||||
| * pure pass-through unless a developer explicitly opts in. Import the auth hooks | ||||||||||||
| * from here (`@/lib/auth`) instead of `@clerk/tanstack-react-start` so the | ||||||||||||
| * bypass reaches every call site. | ||||||||||||
| */ | ||||||||||||
| export const DEV_AUTH = env.VITE_ENABLE_DEV_AUTH === "true"; | ||||||||||||
| export const DEV_USER_ID = "dev-user"; | ||||||||||||
|
|
||||||||||||
| // Dev stubs — shapes mirror the subset of each Clerk hook's return the app reads. | ||||||||||||
| const devUseAuth = (() => ({ | ||||||||||||
| isLoaded: true, | ||||||||||||
| isSignedIn: true, | ||||||||||||
| userId: DEV_USER_ID, | ||||||||||||
| sessionId: "dev-session", | ||||||||||||
| orgId: null, | ||||||||||||
| orgRole: null, | ||||||||||||
| getToken: async () => "dev-auth", | ||||||||||||
| signOut: async () => {}, | ||||||||||||
| })) as unknown as typeof clerkUseAuth; | ||||||||||||
|
|
||||||||||||
| const devUseUser = (() => ({ | ||||||||||||
| isLoaded: true, | ||||||||||||
| isSignedIn: true, | ||||||||||||
| user: { | ||||||||||||
| id: DEV_USER_ID, | ||||||||||||
| fullName: "Dev User", | ||||||||||||
| firstName: "Dev", | ||||||||||||
| lastName: "User", | ||||||||||||
| primaryEmailAddress: { emailAddress: "dev@localhost" }, | ||||||||||||
| imageUrl: "", | ||||||||||||
| }, | ||||||||||||
| })) as unknown as typeof clerkUseUser; | ||||||||||||
|
Comment on lines
+37
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The Fix: Add no-op stubs for the missing methods in user: {
id: DEV_USER_ID,
fullName: "Dev User",
firstName: "Dev",
lastName: "User",
primaryEmailAddress: { emailAddress: "dev@localhost" },
imageUrl: "",
emailAddresses: [],
createEmailAddress: async () => { throw new Error("Not supported in dev mode"); },
reload: async () => {},
},(The Princeton email flow is inherently non-functional without real Clerk, so throwing on |
||||||||||||
|
|
||||||||||||
| const devUseClerk = (() => ({ | ||||||||||||
| signOut: async () => {}, | ||||||||||||
| openSignIn: () => {}, | ||||||||||||
| openUserProfile: () => {}, | ||||||||||||
| })) as unknown as typeof clerkUseClerk; | ||||||||||||
|
|
||||||||||||
| // useReverification wraps an action that may need step-up auth; in dev, pass through. | ||||||||||||
| const devUseReverification = (<T>(fn: T) => | ||||||||||||
| fn) as unknown as typeof clerkUseReverification; | ||||||||||||
|
|
||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Harness/apps/web/src/components/sandbox-result.tsx Lines 1 to 3 in e179f3c
In dev mode The fix is a one-line import swap in
Suggested change
|
||||||||||||
| export const useAuth = DEV_AUTH ? devUseAuth : clerkUseAuth; | ||||||||||||
| export const useUser = DEV_AUTH ? devUseUser : clerkUseUser; | ||||||||||||
| export const useClerk = DEV_AUTH ? devUseClerk : clerkUseClerk; | ||||||||||||
| export const useReverification = DEV_AUTH | ||||||||||||
| ? devUseReverification | ||||||||||||
| : clerkUseReverification; | ||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DEV_AUTHis not actually a build-time constant — dev stubs ship in every production bundle.The comment claims
DEV_AUTHis a build-time constant so the stubs are tree-shaken in production. This is not the case. Vite's static substitution only fires when the literal token sequenceimport.meta.env.VITE_*appears in source. HereDEV_AUTHis read via:followed by
createEnv(...)from@t3-oss/env-core— a runtime function call whose return value Vite cannot inline. Soenv.VITE_ENABLE_DEV_AUTHis a runtime property read, not a static token Vite replaces.As a result:
devUseAuth / devUseUser / devUseClerk / devUseReverificationstubs are retained in every production bundle.useAuth = DEV_AUTH ? devUseAuth : clerkUseAuthternaries are evaluated at runtime, not eliminated at build time.VITE_ENABLE_DEV_AUTH=trueat build time activates the full auth bypass (isSignedIn: truealways,getTokenreturns"dev-auth") in production.Fix: Read the env var directly in this file so Vite can inline it:
With direct access, Vite inlines
falsefor production builds (where the var is unset), allowing the bundler to dead-code-eliminate all four dev stubs.See:
Harness/apps/web/src/lib/auth.ts
Lines 13 to 21 in 3e86a49