chore(repo): add auth({ or }) accessor discrimination POC#8749
Draft
jacekradko wants to merge 1 commit into
Draft
chore(repo): add auth({ or }) accessor discrimination POC#8749jacekradko wants to merge 1 commit into
jacekradko wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: 1cff95d The changes in this PR will be included in the next version bump. This PR includes changesets to release 0 packagesWhen changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
@clerk/astro
@clerk/backend
@clerk/chrome-extension
@clerk/clerk-js
@clerk/expo
@clerk/expo-passkeys
@clerk/express
@clerk/fastify
@clerk/hono
@clerk/localizations
@clerk/nextjs
@clerk/nuxt
@clerk/react
@clerk/react-router
@clerk/shared
@clerk/tanstack-react-start
@clerk/testing
@clerk/ui
@clerk/upgrade
@clerk/vue
commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Draft, not for merge. A typed, runnable proof-of-concept for an
auth({ or })idea that came out of looking at how Stack Auth shapesgetUser({ or }): drive the gate with anoroption and let the return type narrow on the literal, so there's noUser | nullto deal with at the call site once you've writtenor: 'redirect'.The first thing worth poking at is that Clerk already hands you that guarantee, just split across two functions:
auth()is the nullable door,auth.protect()is the guaranteed one. Sooronly buys something where nullability actually lives, which is the accessor. Putting it onprotectadds an option to the one surface that already always narrows. This POC putsoronauth()instead and checks whether the type system can carry the guarantee.It can, and more cleanly than I expected going in. A single generic call signature with a conditional return type absorbs both the
oraxis and the existing token axis, so there's no overload blow-up against the six overloadsprotectcarries today. The return type keys solely on the unauthenticated outcome (authorization failure never returns, so it never enters the type), which is what lets the object formor: { unauthenticated, unauthorized }keep the redirect-anon-but-404-the-signed-in-non-admin behavior a single scalar would flatten, while still narrowing. Bareauth()stays type-identical to today, so no existing call site shifts. Andprotectfalls out as a one-line preset,auth({ or: 'notFound' }).poc/auth-or-poc.tsis self-contained: minimal mirrors of the real auth-object shapes, with file references back to the originals. Every negative case is pinned by@ts-expect-error, and it runs 15 behavioral scenarios.Things I left open on purpose, since they're judgment calls:
orcurrently type-checks and silently doesn't gate at runtime (you get the signed-in object back even when the role check failed). Requiringorwhenever a role or permission is present would close that.auth()already names this optionacceptsTokenwhileprotectcalls ittoken; a unified accessor is a chance to settle on one name.orsupersedesunauthenticatedUrl/unauthorizedUrlor layers over them. The POC composes them.redirect/notFoundare Next primitives andprotectonly ships there today.