-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix: add optionalAuthProcedure to prevent UNAUTHORIZED errors on public pages (#3051) #3085
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: main
Are you sure you want to change the base?
fix: add optionalAuthProcedure to prevent UNAUTHORIZED errors on public pages (#3051) #3085
Conversation
|
@themavik is attempting to deploy a commit to the Onlook Team on Vercel. A member of the Team first needs to authorize it. |
📝 WalkthroughWalkthroughIntroduce a new optional-authentication procedure and corresponding endpoints to gracefully handle unauthenticated requests. The system now returns null for missing user context instead of throwing auth errors, enabling components like telemetry and pricing displays to function without forced authentication. Changes
Sequence Diagram(s)sequenceDiagram
participant Component as Auth-Optional Component
participant Procedure as optionalAuthProcedure
participant Context as createTRPCContext
participant Endpoint as getOptional Endpoint
participant DB as Database
rect rgba(200, 150, 100, 0.5)
Note over Component,DB: Unauthenticated User Flow
Component->>Procedure: Call endpoint (no auth token)
Procedure->>Context: Request context creation
Context-->>Procedure: ctx.user = null (no error thrown)
Procedure->>Endpoint: Pass ctx with user = null
Endpoint->>Endpoint: Check ctx.user is null
Endpoint-->>Component: Return null gracefully
end
rect rgba(100, 150, 200, 0.5)
Note over Component,DB: Authenticated User Flow
Component->>Procedure: Call endpoint (with auth token)
Procedure->>Context: Request context creation
Context->>DB: Fetch authenticated user
DB-->>Context: User data retrieved
Context-->>Procedure: ctx.user populated
Procedure->>Endpoint: Pass ctx with valid user
Endpoint->>DB: Query user/subscription data
DB-->>Endpoint: Return data
Endpoint-->>Component: Return composed result
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 🧹 Recent nitpick comments
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
Fixes #3051.
Root cause:
createTRPCContext()throwsTRPCError({ code: 'UNAUTHORIZED' })whensupabase.auth.getUser()returns an error, which happens for ALL unauthenticated visitors. This means evenpublicProcedureendpoints fail for visitors on marketing pages. Components like the telemetry provider, top bar auth button, and pricing table calluser.getandsubscription.get(protected endpoints), generating cascading UNAUTHORIZED errors and retries.Fix: Three changes:
trpc.ts—createTRPCContext(): Don't throw on auth errors. Instead, setusertonullfor unauthenticated visitors.protectedProcedurestill validates auth via its middleware, so existing protected endpoints are unaffected.trpc.ts— newoptionalAuthProcedure: A new procedure type that passes through the user context as-is (may benull). Use this for endpoints where authentication is optional.user/user.tsandsubscription/subscription.ts: AddedgetOptionalendpoints usingoptionalAuthProcedurethat returnnullfor unauthenticated users instead of throwing. Components on marketing/public pages should useuser.getOptionalandsubscription.getOptionalinstead of the protected variants.Changes
apps/web/client/src/server/api/trpc.ts:createTRPCContext(): Catch auth errors gracefully, setusertonullinstead of throwing.optionalAuthProcedureexport for auth-optional endpoints.apps/web/client/src/server/api/routers/user/user.ts:getOptionalquery that returns user data if authenticated,nullotherwise.apps/web/client/src/server/api/routers/subscription/subscription.ts:getOptionalquery that returns subscription data if authenticated,nullotherwise.Migration Guide
Components that currently call
user.getorsubscription.geton pages accessible to unauthenticated visitors should switch touser.getOptional/subscription.getOptional:Root Cause Analysis
The tRPC context creation was designed assuming all API requests come from authenticated users. When the app expanded to include marketing pages with shared components (telemetry, auth button, pricing), these components' tRPC calls failed at the context level before reaching any procedure middleware. The
protectedProceduremiddleware was correctly checking auth, but the context creation was throwing first.Risk Assessment
protectedProcedurestill enforces auth via its own middleware. Existing protected endpoints continue to reject unauthenticated requests.publicProcedurenow works for unauthenticated users: This is the correct behavior — public procedures should not require auth.getOptionalinstead ofget. This PR provides the backend support; frontend migration can be done incrementally.Important
Introduces
optionalAuthProcedureto handle unauthenticated users gracefully and adds optional auth endpoints in user and subscription routers.createTRPCContext()intrpc.ts: Setsusertonullfor unauthenticated users instead of throwing an error.optionalAuthProcedureintrpc.tsfor endpoints where authentication is optional.user.ts: AddsgetOptionalendpoint usingoptionalAuthProcedureto return user data ornull.subscription.ts: AddsgetOptionalendpoint usingoptionalAuthProcedureto return subscription data ornull.This description was created by
for 1dad797. You can customize this summary. It will automatically update as commits are pushed.
Summary by CodeRabbit
Release Notes