Skip to content

fix(astro): Middleware return type - #3792

Merged
wobsoriano merged 3 commits into
mainfrom
fix-astro-middleware-return-type
Jul 24, 2024
Merged

fix(astro): Middleware return type#3792
wobsoriano merged 3 commits into
mainfrom
fix-astro-middleware-return-type

Conversation

@wobsoriano

@wobsoriano wobsoriano commented Jul 23, 2024

Copy link
Copy Markdown
Member

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:

@changeset-bot

changeset-bot Bot commented Jul 23, 2024

Copy link
Copy Markdown

🦋 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
Comment thread .changeset/ninety-squids-appear.md Outdated
"@clerk/astro": patch
---

Fix middleware return type.

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
@wobsoriano
wobsoriano deleted the fix-astro-middleware-return-type branch July 24, 2024 16:02
@softbeehive

Copy link
Copy Markdown

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

wobsoriano commented Jul 24, 2024

Copy link
Copy Markdown
Member Author

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
Copy Markdown

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