-
Notifications
You must be signed in to change notification settings - Fork 282
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
Conversation
🦋 Changeset detectedLatest commit: 6c4c96d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
.changeset/ninety-squids-appear.md
Outdated
"@clerk/astro": patch | ||
--- | ||
|
||
Fix middleware return type. |
There was a problem hiding this comment.
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()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks Pantelis
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 I used this doc as a guide 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. |
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 A PR is also open for opting-out of telemetry. |
Cheers, +1 ⭐ |
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.This small PR addresses the issue.
Checklist
npm test
runs as expected.npm run build
runs as expected.Type of change