-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(nextjs): Support unstable_rethrow inside clerkMiddleware (#4347)
Co-authored-by: Jacek <[email protected]>
- Loading branch information
1 parent
1b32955
commit 7149cc2
Showing
9 changed files
with
232 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@clerk/nextjs": major | ||
--- | ||
|
||
Support `unstable_rethrow` inside `clerkMiddleware`. | ||
We changed the errors thrown by `protect()` inside `clerkMiddleware` in order for `unstable_rethrow` to recognise them and rethrow them. |
This file contains 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
This file contains 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
3 changes: 3 additions & 0 deletions
3
integration/templates/next-app-router/src/app/only-admin/page.tsx
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function Page() { | ||
return <div>User is admin</div>; | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/** | ||
* Clerk's identifiers that are used alongside the ones from Next.js | ||
*/ | ||
const CONTROL_FLOW_ERROR = { | ||
FORCE_NOT_FOUND: 'CLERK_PROTECT_REWRITE', | ||
REDIRECT_TO_URL: 'CLERK_PROTECT_REDIRECT_TO_URL', | ||
REDIRECT_TO_SIGN_IN: 'CLERK_PROTECT_REDIRECT_TO_SIGN_IN', | ||
}; | ||
|
||
/** | ||
* In-house implementation of `notFound()` | ||
* https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/not-found.ts | ||
*/ | ||
const NOT_FOUND_ERROR_CODE = 'NEXT_NOT_FOUND'; | ||
|
||
type NotFoundError = Error & { | ||
digest: typeof NOT_FOUND_ERROR_CODE; | ||
clerk_digest: typeof CONTROL_FLOW_ERROR.FORCE_NOT_FOUND; | ||
}; | ||
|
||
function isNextjsNotFoundError(error: unknown): error is NotFoundError { | ||
if (typeof error !== 'object' || error === null || !('digest' in error)) { | ||
return false; | ||
} | ||
|
||
return error.digest === NOT_FOUND_ERROR_CODE; | ||
} | ||
|
||
function nextjsNotFound(): never { | ||
const error = new Error(NOT_FOUND_ERROR_CODE); | ||
(error as NotFoundError).digest = NOT_FOUND_ERROR_CODE; | ||
(error as NotFoundError).clerk_digest = CONTROL_FLOW_ERROR.FORCE_NOT_FOUND; | ||
throw error; | ||
} | ||
|
||
/** | ||
* In-house implementation of `redirect()` | ||
* https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/redirect.ts | ||
*/ | ||
|
||
const REDIRECT_ERROR_CODE = 'NEXT_REDIRECT'; | ||
|
||
type RedirectError<T = unknown> = Error & { | ||
digest: `${typeof REDIRECT_ERROR_CODE};${'replace'};${string};${307};`; | ||
clerk_digest: typeof CONTROL_FLOW_ERROR.REDIRECT_TO_URL | typeof CONTROL_FLOW_ERROR.REDIRECT_TO_SIGN_IN; | ||
} & T; | ||
|
||
function nextjsRedirectError( | ||
url: string, | ||
extra: Record<string, unknown>, | ||
type: 'replace' = 'replace', | ||
statusCode: 307 = 307, | ||
): never { | ||
const error = new Error(REDIRECT_ERROR_CODE) as RedirectError; | ||
error.digest = `${REDIRECT_ERROR_CODE};${type};${url};${statusCode};`; | ||
error.clerk_digest = CONTROL_FLOW_ERROR.REDIRECT_TO_URL; | ||
Object.assign(error, extra); | ||
throw error; | ||
} | ||
|
||
function redirectToSignInError(url: string, returnBackUrl?: string | URL | null): never { | ||
nextjsRedirectError(url, { | ||
clerk_digest: CONTROL_FLOW_ERROR.REDIRECT_TO_SIGN_IN, | ||
returnBackUrl: returnBackUrl === null ? '' : returnBackUrl || url, | ||
}); | ||
} | ||
|
||
/** | ||
* Checks an error to determine if it's an error generated by the | ||
* `redirect(url)` helper. | ||
* | ||
* @param error the error that may reference a redirect error | ||
* @returns true if the error is a redirect error | ||
*/ | ||
function isNextjsRedirectError(error: unknown): error is RedirectError<{ redirectUrl: string | URL }> { | ||
if (typeof error !== 'object' || error === null || !('digest' in error) || typeof error.digest !== 'string') { | ||
return false; | ||
} | ||
|
||
const digest = error.digest.split(';'); | ||
const [errorCode, type] = digest; | ||
const destination = digest.slice(2, -2).join(';'); | ||
const status = digest.at(-2); | ||
|
||
const statusCode = Number(status); | ||
|
||
return ( | ||
errorCode === REDIRECT_ERROR_CODE && | ||
(type === 'replace' || type === 'push') && | ||
typeof destination === 'string' && | ||
!isNaN(statusCode) && | ||
statusCode === 307 | ||
); | ||
} | ||
|
||
function isRedirectToSignInError(error: unknown): error is RedirectError<{ returnBackUrl: string | URL }> { | ||
if (isNextjsRedirectError(error) && 'clerk_digest' in error) { | ||
return error.clerk_digest === CONTROL_FLOW_ERROR.REDIRECT_TO_SIGN_IN; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
export { | ||
isNextjsNotFoundError, | ||
nextjsNotFound, | ||
redirectToSignInError, | ||
nextjsRedirectError, | ||
isNextjsRedirectError, | ||
isRedirectToSignInError, | ||
}; |