I shipped skillcraft.ai !!!
Skillcraft helps you find the best learning resources tailored to your goals. Get a personalized roadmap with the best courses, books, and tutorials. Try it out, for free!
Up to date
Published
4 min read

Trevor I. Lasn

Building tools for developers. Currently building skillcraft.ai and blamesteve.lol

Add Auth to Astro 5 with Clerk in 5 Minutes

The simplest setup for adding Clerk authentication to your Astro project, with minimal code

Authentication has always been a pain point. Either you spend weeks building a robust auth system, or you piece together various services that don’t quite fit. That’s why I was excited to discover how well Astro and Clerk work together.

Why Astro?

Astro’s content-focused approach means I can build fast, SEO-friendly sites without shipping unnecessary JavaScript. It’s perfect for my personal site (trevorlasn.com) because it delivers exactly what I need: fast load times and seamless content management.

What really sold me on Astro was its “islands architecture”; I can write most of my site in static HTML and sprinkle in interactivity only where needed. This means my authentication UI can be dynamic while keeping the rest of the site lightweight.

Astro’s “islands” of interactivity give me the best of both worlds: the speed of static HTML with the power of React when I actually need it.

Why Clerk?

Authentication is complex. You need to handle sessions, manage tokens, deal with OAuth providers, implement 2FA, and much more. Clerk handles all of this out of the box, and their free tier is generous - 10,000 monthly active users is more than enough for most projects.

The integration with Astro is surprisingly straightforward. A few lines of code, and you have a complete auth system.

Getting Started

First, start an Astro project from scratch. If you want to see the code first, you can find it here

Next, install Clerk.

Finally, add Clerk to your Astro project.

Set your Clerk API keys. You can find these in your Clerk dashboard.

Middleware is required to protect your routes. Add the following to your middleware.ts file.

You can customize the routes Clerk protects by modifying the createRouteMatcher array. For this example, Clerk will protect the /dashboard route.

That’s all that’s needed to setup the functionality. Next, let’s add the UI components.

Clerk provides SignedOut, SignedIn, SignInButton and UserButton components. These components make it easy to build a custom authentication UI.

  • 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.
  • SignInButton: An unstyled component that links to the sign-in page.

Start by creating a route that will be accessible to all users.

Next, let’s create a protected route. This route will only be accessible to signed-in users. Remember, we protected the /dashboard route in the middleware.ts file.

Finally, we can test our setup. Run the following command to start the Astro server.

  • '/' should display the sign-in button.

Home view

Clicking the sign-in button should open the Clerk modal.

Clerk signup modal

After signing in, you should see the signed in view. The sign-in button should be replaced with the user profile image.

Signed in home view

  • If everything went smoothly, the '/dashboard' route should be accessible only to signed-in users. Otherwise, you’ll be redirected to the sign-in page.

Dashboard view

When we head to the Clerk dashboard, we can see the user we just created.

Clerk dashboard

That’s it! You now have a fully functional authentication system in your Astro project. You can customize the UI, add 2FA, and much more. Clerk’s documentation is excellent, so you’ll have no trouble getting started.


Found this article helpful? You might enjoy my free newsletter. I share dev tips and insights to help you grow your coding skills and advance your tech career.


Check out these related articles that might be useful for you. They cover similar topics and provide additional insights.

Webdev
3 min read

CSS content-visibility: The Web Performance Boost You Might Be Missing

The content-visibility CSS property delays rendering an element, including layout and painting, until it is needed

Dec 5, 2024
Read article
Webdev
4 min read

Speed Up Your Website With rel='preconnect' and increase PageSpeed Insights Score

Using link rel='preconnect' can improve your website's performance by reducing connection setup times to key external domains.

Sep 13, 2024
Read article
Webdev
4 min read

LH and RLH: The CSS Units That Make Vertical Spacing Easy

Exploring new CSS line-height units that eliminate guesswork from vertical rhythm

Dec 3, 2024
Read article
Webdev
3 min read

CVE-2025-29927 - Next.js Middleware Bypass Explained In Simple Terms

The vulnerability skips Next.js middleware security checks by adding a single HTTP header

Apr 6, 2025
Read article
Webdev
3 min read

Improve PageSpeed Insights Score with Lazy Loading Iframes

How to save bandwidth and speed up your site by lazy-loading iframes

Sep 13, 2024
Read article
Webdev
13 min read

10 Essential Terminal Commands Every Developer Should Know

List of useful Unix terminal commands to boost your productivity. Here are some of my favorites.

Aug 21, 2024
Read article
Webdev
7 min read

Tips for Reducing Cyclomatic Complexity

Cyclomatic complexity is like counting how many ways a car can go. More options make it harder to drive because you have to make more decisions, which can lead to confusion.

Sep 10, 2024
Read article
Webdev
5 min read

SecretLint — A Linter for Preventing Committing Credentials

A guide to catching and preventing credential leaks in your code using Secretlint

Oct 22, 2024
Read article
Webdev
4 min read

Remove Unnecessary NPM Packages with eslint-plugin-depend

We don't need packages to handle basic JavaScript tasks

Aug 13, 2024
Read article

This article was originally published on https://www.trevorlasn.com/blog/astro-and-clerk. It was written by a human and polished using grammar tools for clarity.