Skip to content

Commit

Permalink
Merge pull request #279 from ixartz/clerk-middleware
Browse files Browse the repository at this point in the history
feat: vscode jest open test result view on test fails and add unauthenticatedUrl in clerk middleware
  • Loading branch information
ixartz committed May 17, 2024
2 parents 98a092f + 2a68124 commit e6ecff6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 9 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,22 @@
"typescript.preferences.autoImportFileExcludePatterns": [
// useRouter should be imported from `next/navigation` instead of `next/router`
"next/router.d.ts",
"next/dist/client/router.d.ts"
"next/dist/client/router.d.ts",
// give priority for Link to next/link instead of lucide-react
"lucide-react"
],
"typescript.preferences.preferTypeOnlyAutoImports": true, // Prefer type-only imports
"jest.runMode": {
"type": "on-save",
"testFileOnly": true,
"coverage": true
}, // Configure Jest extension
"jest.outputConfig": {
"revealOn": "error",
"revealWithFocus": "test-results",
"clearOnRun": "none"
}, // Switch to test-results view when test fails
"testing.openTesting": "neverOpen", // For a consistent Jest output experience, the simplest solution is to set to "neverOpen"
// Multiple language settings for json and jsonc files
"[json][jsonc][yaml]": {
"editor.formatOnSave": true,
Expand Down
13 changes: 12 additions & 1 deletion src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,24 @@ export default function middleware(
request: NextRequest,
event: NextFetchEvent,
) {
// Run Clerk middleware only when it's necessary
if (
request.nextUrl.pathname.includes('/sign-in') ||
request.nextUrl.pathname.includes('/sign-up') ||
isProtectedRoute(request)
) {
return clerkMiddleware((auth, req) => {
if (isProtectedRoute(req)) auth().protect();
if (isProtectedRoute(req)) {
const locale =
req.nextUrl.pathname.match(/(\/.*)\/dashboard/)?.at(1) ?? '';

const signInUrl = new URL(`${locale}/sign-in`, req.url);

auth().protect({
// `unauthenticatedUrl` is needed to avoid error: "Unable to find `next-intl` locale because the middleware didn't run on this request"
unauthenticatedUrl: signInUrl.toString(),
});
}

return intlMiddleware(req);
})(request, event);
Expand Down

0 comments on commit e6ecff6

Please sign in to comment.