Astro Quickstart
Before you start
Example repository
Create a new Astro app
If you don't already have an Astro app, run the following commands to create a new one.
npm create astro@latest clerk-astro
cd clerk-astro
npm installpnpm create astro clerk-astro
cd clerk-astro
pnpm installyarn create astro clerk-astro
cd clerk-astro
yarn installbunx create-astro clerk-astro
cd clerk-astro
bun installInstall @clerk/astro
The Clerk Astro SDK gives you access to a set of components, hooks, and stores to make user authentication easier.
Run the following command to install the SDK:
npm install @clerk/astropnpm add @clerk/astroyarn add @clerk/astrobun add @clerk/astroAdd the following keys to your .env file. These keys can always be retrieved from the API keys page in the Clerk Dashboard.
- In the Clerk Dashboard, navigate to the API keys page.
- In the Quick Copy section, copy your Clerk and .
- Paste your keys into your
.envfile.
The final result should resemble the following:
PUBLIC_CLERK_PUBLISHABLE_KEY=YOUR_PUBLISHABLE_KEY
CLERK_SECRET_KEY=YOUR_SECRET_KEYUpdate astro.config.mjs
To configure Clerk in your Astro app, you will need to update your astro.config.mjs.
- Add the
clerk()integration to theintegrationslist. For a list of available options, see the integration reference. - Install an SSR adapter. This quickstart uses the
@astrojs/nodeadapter. - Set
outputtoserver. This is required when deploying to a host supporting SSR.
import { defineConfig } from 'astro/config'
import node from '@astrojs/node'
import clerk from '@clerk/astro'
export default defineConfig({
integrations: [clerk()],
adapter: node({ mode: 'standalone' }),
output: 'server',
})Add clerkMiddleware() to your app
clerkMiddleware() grants you access to user authentication state throughout your app. It also allows you to protect specific routes from unauthenticated users. To add clerkMiddleware() to your app, follow these steps:
- Create a
middleware.tsfile.- If you're using the
/srcdirectory, createmiddleware.tsin the/srcdirectory. - If you're not using the
/srcdirectory, createmiddleware.tsin the root directory alongside.env.
- If you're using the
- In your
middleware.tsfile, export anonRequestconstant and assign the result of theclerkMiddleware()function to it.src /middleware.ts import { clerkMiddleware } from '@clerk/astro/server' export const onRequest = clerkMiddleware() - By default,
clerkMiddleware()will not protect any routes. All routes are public and you must opt-in to protection for routes. See theclerkMiddleware()reference to learn how to require authentication for specific routes.
Create a header with Clerk components
You can control which content signed-in and signed-out users can see with Clerk's prebuilt control components. The following example creates 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.
- <SignInButton />: An unstyled component that links to the sign-in page. In this example, since no props or environment variables are set for the sign-in URL, this component links to the Account Portal sign-in page.
- <SignUpButton />: An unstyled component that links to the sign-up page. In this example, since no props or environment variables are set for the sign-up URL, this component links to the Account Portal sign-up page.
---
import {
SignedIn,
SignedOut,
UserButton,
SignInButton,
SignUpButton,
} from '@clerk/astro/components'
---
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="generator" content={Astro.generator} />
<title>Astro Basics</title>
</head>
<body>
<header>
{/* Show the sign-in and sign-up buttons when the user is signed out */}
<SignedOut>
<SignInButton mode="modal" />
<SignUpButton mode="modal" />
</SignedOut>
{/* Show the user button when the user is signed in */}
<SignedIn>
<UserButton />
</SignedIn>
</header>
<slot />
</body>
</html>
8 lines collapsed
<style>
html,
body {
margin: 0;
width: 100%;
height: 100%;
}
</style>Then, use the layout on your homepage:
---
import Layout from '../layouts/Layout.astro'
import { SignedIn, SignedOut } from '@clerk/astro/components'
---
<Layout title="Clerk + Astro">
<SignedOut>
<p>Sign in to try Clerk out!</p>
</SignedOut>
<SignedIn>
<p>You are signed in!</p>
</SignedIn>
</Layout>Run your project
Run your project with the following command:
npm run devpnpm run devyarn devbun run devCreate your first user
- Visit your app's homepage at http://localhost:4321.
- Select "Sign up" on the page and authenticate to create your first user.
Next steps
Learn more about Clerk's stores, how to use them to access user data, and how to use Clerk's client-side helpers using the following guides.
Protect routes using Clerk Middleware
Learn how to protect specific routes from unauthenticated users.
Protect content and read user data
Learn how to use Clerk's stores and helpers to protect content and read user data in your Astro app.
Use Clerk with Astro and React
Learn how to set up your Astro app to be integrated with React.
Clerk + Astro Quickstart Repo
The official companion repo for Clerk's Astro Quickstart.
Feedback
Last updated on