The Clerk Astro SDKAstro Icon provides a set of components, hooks, and stores that make it easy to build authentication and user management features in your Astro app.
clerkMiddleware()Astro Icon grants you access to user authentication state throughout your app, on any route or page. It also allows you to protect specific routes from unauthenticated users. To add clerkMiddleware() to your app, follow these steps:
Create a middleware.ts file.
If you're using the /src directory, create middleware.ts in the /src directory.
If you're not using the /src directory, create middleware.ts in the root directory alongside .env.
In your middleware.ts file, export an onRequest constant and assign the result of the clerkMiddleware() function to it.
src/middleware.ts
By default, clerkMiddleware() will not protect any routes. All routes are public and you must opt-in to protection for routes. See the clerkMiddleware() referenceAstro Icon to learn how to require authentication for specific routes.
You can control which content signed-in and signed-out users can see with Clerk's prebuilt control components. Create a header using the following components:
<SignedIn>: Children of this component can only be seen while signed in.
<SignedOut>: Children of this component can only be seen while signed out.
<UserButton />: Shows the signed-in user's avatar. Selecting it opens a dropdown menu with account management options.