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

fix(astro): Middleware return type #3792

Merged
merged 3 commits into from
Jul 24, 2024
Merged

Conversation

wobsoriano
Copy link
Member

@wobsoriano wobsoriano commented Jul 23, 2024

Description

The middleware contains logic to check for a passed handler. When no handler is provided, it automatically executes the next function. However, the return type doesn't handle this scenario and still expects us to call the next function. It should be optional.

Screenshot 2024-07-23 at 3 27 01 PM

This small PR addresses the issue.

Checklist

  • npm test runs as expected.
  • npm run 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

changeset-bot bot commented Jul 23, 2024

🦋 Changeset detected

Latest commit: 6c4c96d

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

This PR includes changesets to release 1 package
Name Type
@clerk/astro 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

@wobsoriano wobsoriano requested a review from panteliselef July 23, 2024 22:31
@wobsoriano wobsoriano marked this pull request as ready for review July 23, 2024 22:37
"@clerk/astro": patch
---

Fix middleware return type.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allow the handler of clerkMiddleware to return undefined. When undefined is returned clerkMiddleware implicitly calls await next()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks Pantelis

@wobsoriano wobsoriano merged commit b963821 into main Jul 24, 2024
17 checks passed
@wobsoriano wobsoriano deleted the fix-astro-middleware-return-type branch July 24, 2024 16:02
@softbeehive
Copy link

Hi folks,

I'm integrating clerk with Astro v4.12.2. May I ask why do you use default export? Astro documentation states that middleware is defined by exporting onRequest

I used this doc as a guide
https://clerk.com/docs/references/astro/clerk-middleware#protect-routes-based-on-user-authentication-status

I had an impression it was copied from next.js without much testing. Default export was throwing errors, because Astro expects a Response. I ended up doing the following:

import { defineMiddleware, sequence } from 'astro:middleware'
import { clerkMiddleware } from '@clerk/astro/server'

const authMiddleware = defineMiddleware(async (context, next) => {
  // Protect these pages
  const protectedPages = ['/plan']
  const { url } = context

  // Skip auth for public pages
  if (!protectedPages.some((pagePath) => url.pathname.startsWith(pagePath))) {
    return next()
  }

  const auth = await context.locals?.auth()
  if (auth.userId == null) {
    return auth.redirectToSignIn()
  }

  return next()
})

export const onRequest = sequence(clerkMiddleware(), authMiddleware)

I also dug deeper to discover that Astro intergration telemetry collection is not configurable. I recognize this integration is fresh. Consider making it respect CLERK_TELEMETRY_DEBUG. I learned new things but I'd appreciate the seamless quickstart experience, hopefully other folks can benefit from my struggle.

@wobsoriano
Copy link
Member Author

wobsoriano commented Jul 24, 2024

Thank you for reporting the incorrect exports @softbeehive. I've opened a pull request to address this docs issue here.

The way to export and use the middleware (which can be seen in the astro quickstart) is as follows:

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

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

export const onRequest = clerkMiddleware((auth, context) => {
  const { redirectToSignIn, userId } = auth();

  if (!userId && isProtectedRoute(context.request)) {
    return redirectToSignIn();
  }
});

But your example using sequence also works.

A PR is also open for opting-out of telemetry.

@softbeehive
Copy link

Cheers, +1 ⭐

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants