Skip to content

Conversation

@LauraBeatris
Copy link
Member

@LauraBeatris LauraBeatris commented Mar 23, 2025

Description

Resolves ORGS-582

If clerkMiddleware executes auth.protect(), by default it'll look for the sts on session claims and redirect to tasks.

The index path on SessionTasks performs a reload on the client session, to resolve the missing task resource and then proceeds redirecting to the next component route.

import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server';

const isProtectedRoute = createRouteMatcher(['/home(.*)'])

export default clerkMiddleware(async (auth, req) => {
  if (isProtectedRoute(req)) {
    // Navigates to tasks by default if the user session has pending status
    await auth.protect()
  }
})

export const config = {
  matcher: ['/((?!.*\\..*|_next).*)', '/', '/(api|trpc)(.*)'],
};

Behavior

CleanShot.2025-03-24.at.14.23.16.mp4

Inner workings

The server cannot redirect directly to the specific task URL, eg: /sign-in/tasks/add-organization since it only has the sts JWT claim.

It verifies if sts is pending, and if so, navigates to /sign-in/tasks. Once clerk-js loads, it verifies for the Clerk.session.currentTask and performs a further client-side navigation to the specific task route.

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

@vercel
Copy link

vercel bot commented Mar 23, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
clerk-js-sandbox ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 1, 2025 7:42pm

@changeset-bot
Copy link

changeset-bot bot commented Mar 23, 2025

🦋 Changeset detected

Latest commit: 2028869

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 11 packages
Name Type
@clerk/backend Minor
@clerk/nextjs Minor
@clerk/astro Minor
@clerk/agent-toolkit Patch
@clerk/express Patch
@clerk/fastify Patch
@clerk/nuxt Patch
@clerk/react-router Patch
@clerk/remix Patch
@clerk/tanstack-react-start Patch
@clerk/testing Patch

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

@LauraBeatris LauraBeatris force-pushed the laura/auth-protect-for-tasks branch from cc1d782 to 6916509 Compare March 24, 2025 00:12
@LauraBeatris LauraBeatris changed the title (wip) chore(nextjs,backend): Redirect to tasks on auth.protect (wip) chore(nextjs,backend): Redirect to tasks on auth.protect() Mar 24, 2025
@LauraBeatris LauraBeatris force-pushed the laura/auth-protect-for-tasks branch from 53870e2 to c4d287d Compare March 24, 2025 00:17
@LauraBeatris LauraBeatris changed the title (wip) chore(nextjs,backend): Redirect to tasks on auth.protect() chore(nextjs,backend): Redirect to tasks on auth.protect() Mar 24, 2025
@LauraBeatris LauraBeatris marked this pull request as ready for review March 24, 2025 00:23
@LauraBeatris LauraBeatris requested a review from a team March 24, 2025 00:23
@LauraBeatris LauraBeatris self-assigned this Mar 24, 2025
@LauraBeatris LauraBeatris force-pushed the laura/auth-protect-for-tasks branch from c4d287d to 1dcecd5 Compare March 24, 2025 00:26
@LauraBeatris LauraBeatris force-pushed the laura/lazy-load-tasks-env branch 4 times, most recently from 79831ab to 4dde647 Compare March 24, 2025 11:55
@LauraBeatris LauraBeatris force-pushed the laura/auth-protect-for-tasks branch from 1dcecd5 to d74efe3 Compare March 24, 2025 12:07
@LauraBeatris LauraBeatris changed the title chore(nextjs,backend): Redirect to tasks on auth.protect() chore(nextjs,backend): Redirect to tasks on auth.protect and auth.redirectToSignIn Mar 24, 2025
@LauraBeatris LauraBeatris changed the title chore(nextjs,backend): Redirect to tasks on auth.protect and auth.redirectToSignIn chore(nextjs,astro,backend): Redirect to tasks on auth.protect and auth.redirectToSignIn Mar 24, 2025
@LauraBeatris LauraBeatris force-pushed the laura/auth-protect-for-tasks branch from 4c25a67 to 5020678 Compare March 24, 2025 19:35
@LauraBeatris LauraBeatris force-pushed the laura/auth-protect-for-tasks branch from df2b744 to eeafcc5 Compare March 28, 2025 16:38
@LauraBeatris LauraBeatris force-pushed the laura/auth-protect-for-tasks branch from eeafcc5 to a103ab9 Compare March 28, 2025 16:49
@LauraBeatris LauraBeatris changed the title feat(nextjs,astro,backend,types): Redirect to tasks on auth.protect and auth.redirectToSignIn feat(nextjs,astro,backend): Redirect to tasks on auth.protect and auth.redirectToSignIn Mar 28, 2025
@LauraBeatris LauraBeatris force-pushed the laura/auth-protect-for-tasks branch from 710c402 to 611c61f Compare March 28, 2025 16:51
@LauraBeatris LauraBeatris marked this pull request as draft March 31, 2025 17:51
@LauraBeatris LauraBeatris force-pushed the laura/auth-protect-for-tasks branch from fe44cc4 to 2028869 Compare April 1, 2025 19:41
@LauraBeatris LauraBeatris marked this pull request as ready for review April 1, 2025 19:41
@LauraBeatris LauraBeatris merged commit fe065a9 into main Apr 3, 2025
30 checks passed
@LauraBeatris LauraBeatris deleted the laura/auth-protect-for-tasks branch April 3, 2025 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants