Skip to content
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

feat(nextjs,astro,backend,types): Redirect to tasks on auth.protect and auth.redirectToSignIn #5440

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from

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 the pending task to be resolved 
    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:

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 Mar 26, 2025 4:50pm

Copy link

changeset-bot bot commented Mar 23, 2025

🦋 Changeset detected

Latest commit: 5dca566

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

This PR includes changesets to release 22 packages
Name Type
@clerk/backend Minor
@clerk/nextjs Minor
@clerk/astro Minor
@clerk/types 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
@clerk/clerk-js Patch
@clerk/elements Patch
@clerk/expo-passkeys Patch
@clerk/clerk-expo Patch
@clerk/localizations Patch
@clerk/clerk-react Patch
@clerk/shared Patch
@clerk/themes Patch
@clerk/vue Patch
@clerk/chrome-extension 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/lazy-load-tasks-env branch from 4dde647 to 6e7d003 Compare March 26, 2025 15:44
Base automatically changed from laura/lazy-load-tasks-env to main March 26, 2025 16:07
@LauraBeatris
Copy link
Member Author

LauraBeatris commented Mar 26, 2025

FYI - I'm currently introducing unit tests for tasks redirection on clerkMiddleware.test.ts, I've forgot about it and only included integration tests at first

While I'm refactoring, some integration tests might start to fail here as well - I'll ping another review

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.

2 participants