LaunchFast Logo LaunchFast

Authenticating users in Astro with Clerk and React: A Step-by-Step Guide

Rishi Raj Jain
Authenticating users in Astro with Clerk and React

In this guide, you will learn how to authenticate users in an Astro application using Clerk and React. You will go through the process of setting up a new Astro project, enabling server-side rendering using Node.js adapter, and integrating Clerk to authenticate users seamlessly.

Prerequisites

You’ll need the following:

Table Of Contents

Create a new Astro application

Let’s get started by creating a new Astro project. Execute the following command:

npm create astro@latest my-app

npm create astro is the recommended way to scaffold an Astro project quickly.

When prompted, choose:

Once that’s done, you can move into the project directory and start the app:

cd my-app
npm run dev

The app should be running on localhost:4321.

Now, let’s move on to integrate React in your Astro application.

Integrate React in your Astro project

To create dynamic user interfaces with Clerk, you will integrate React in your Astro project. Execute the following command:

npx astro add react

npx allows us to execute npm packages binaries without having to first install it globally.

When prompted, choose the following:

Now, let’s move on to enabling server-side rendering in your Astro application.

Integrate Node.js adapter in your Astro project

To authenticate and maintain user sessions with Clerk, you will enable server-side rendering in your Astro project via the Node.js adapter. Execute the following command:

npx astro add node

When prompted, choose the following:

Now, let’s move on to creating a new Clerk application.

Create a new Clerk application

Create a new Clerk Application Select authentication providers
# .env

# Clerk Environment Variables

PUBLIC_ASTRO_APP_CLERK_PUBLISHABLE_KEY="pk_test_..."
CLERK_SECRET_KEY="sk_test_..."
Clerk Environment Variables

Now, let’s move on to integrate Clerk in your Astro project.

Integrate Clerk in your Astro project

Install the Clerk’s Community SDK for Astro

Execute the command below to install the necessary package for building authentication with Clerk:

npm install astro-clerk-auth

The command installs the following library:

Further, perform the following additions in astro.config.mjs to use the Clerk integration:

// File: astro.config.mjs

import node from "@astrojs/node";
import react from "@astrojs/react";
import clerk from "astro-clerk-auth"; 
import { defineConfig } from "astro/config";

export default defineConfig({
  output: "server",
  adapter: node({
    mode: "standalone",
  }),
  integrations: [
    react(),
    clerk({ 
      afterSignInUrl: "/", 
      afterSignUpUrl: "/", 
    }), 
  ],
});

Intercept all incoming requests using Astro middleware

To make sure that each request maintains a user session accessible over the server-side endpoints, and in .astro pages during server-side rendering, you are going create a middleware that uses Clerk to decode/encode a user session from the cookie.

Create a file middleware.ts in the src directory with the following code:

// File: src/middleware.ts

import { clerkMiddleware } from "astro-clerk-auth/server";

export const onRequest = clerkMiddleware();

Create the index route

Create an App.jsx inside the src directory with the following code:

import { SignedIn, SignedOut, UserButton } from "astro-clerk-auth/client/react";

export default function () {
  return <>
    <SignedOut>
      <a href="/sign-in" className="rounded border px-3 py-1 max-w-max">
        Sign In
      </a>
    </SignedOut>
    <SignedIn>
      <UserButton />
    </SignedIn>
  </>
}

The code above does the following:

To use this React component on the home page of your application, make the following changes in src/pages/index.astro file:

---
// File: src/pages/index.astro

import App from "../App"; 
---

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
    <meta name="viewport" content="width=device-width" />
    <meta name="generator" content={Astro.generator} />
    <title>Astro</title>
  </head>
  <body>
    <App client:load />
  </body>
</html>

The changes above import the App React component and use Astro’s client:load directive to make sure that this React component is hydrated immediately on the page.

Create a custom sign-in page

Create a sign-in.astro file inside the src/pages directory with the following command:

---
// File: src/pages/sign-in.astro

import { SignIn as ClerkSignIn } from "astro-clerk-auth/client/react";
---

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
    <meta name="viewport" content="width=device-width" />
    <meta name="generator" content={Astro.generator} />
    <title>Astro</title>
  </head>
  <body>
    <ClerkSignIn client:load />
  </body>
</html>

The code above does the following:

Build and Test your Astro application locally

To test the application, prepare a build and run the preview server using the command below:

npm run build && node ./dist/server/entry.mjs

Conclusion

In this guide, you enabled user authentication via various methods with the help of Clerk in an Astro application. You’ve also gained some experience with using React in Astro, and understanding how it can help you build dynamic user interfaces.

If you have any questions or comments, feel free to reach out to me on Twitter.

Learn More Using PhotoSwipe in Astro to Build an Image Gallery → Using Transformers for Shiki to enrich Syntax Highlighting in Astro → Using GreenSock Animation Platform (GSAP) in Astro with View Transitions: A Step-by-Step Guide →