Skip to content

Commit

Permalink
Add Clerk Astro guide (#8908)
Browse files Browse the repository at this point in the history
Co-authored-by: Lennart <lekoarts@gmail.com>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
  • Loading branch information
3 people committed Jul 26, 2024
1 parent 92a32f8 commit 16a0c44
Showing 1 changed file with 73 additions and 1 deletion.
74 changes: 73 additions & 1 deletion src/content/docs/en/guides/authentication.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import ReadMore from '~/components/ReadMore.astro'

Authentication and authorization are two security processes that manage access to your website or app. Authentication verifies a visitor's identity, while authorization grants access to protected areas and resources.

Authentication allows you to customize areas of your site for logged-in individuals and provides the greatest protection for personal or private information. Authentication libraries (e.g. [Lucia Auth](https://lucia-auth.com/), [Auth.js](https://authjs.dev/)) provide utilities for multiple authentication methods such as email sign-in and OAuth providers.
Authentication allows you to customize areas of your site for logged-in individuals and provides the greatest protection for personal or private information. Authentication libraries (e.g. [Lucia Auth](https://lucia-auth.com/), [Auth.js](https://authjs.dev/), [Clerk](https://clerk.com)) provide utilities for multiple authentication methods such as email sign-in and OAuth providers.

:::tip
There is no official authentication solution for Astro, but you can find [community "auth" integrations](https://astro.build/integrations/?search=auth) in the integrations directory.
Expand Down Expand Up @@ -188,6 +188,78 @@ const session = await getSession(Astro.request);
- [`auth-astro` on GitHub](https://github.com/nowaythatworked/auth-astro?tab=readme-ov-file#auth-astro)
- [Auth.js documentation](https://authjs.dev/)

## Clerk

Clerk is a complete suite of embeddable UIs, flexible APIs, and admin dashboards to authenticate and manage your users. An [official Clerk SDK for Astro](https://clerk.com/docs/references/astro/overview) is available.

### Installation

Install `@clerk/astro` using the package manager of your choice.

<PackageManagerTabs>
<Fragment slot="npm">
```shell
npm install @clerk/astro
```
</Fragment>
<Fragment slot="pnpm">
```shell
pnpm add @clerk/astro
```
</Fragment>
<Fragment slot="yarn">
```shell
yarn add @clerk/astro
```
</Fragment>
</PackageManagerTabs>

### Configuration

Follow [Clerk's own Astro Quickstart guide](https://clerk.com/docs/quickstarts/astro) to set up Clerk integration and middleware in your Astro project.

### Usage

Clerk provides components that allow you to control the visibility of pages based on your user's authentication state. Show logged out users a sign in button instead of the content available to users who are logged in:

```astro title="src/pages/index.astro"
---
import Layout from 'src/layouts/Base.astro';
import { SignedIn, SignedOut, UserButton, SignInButton } from '@clerk/astro/components';
---
<Layout>
<SignedIn>
<UserButton />
</SignedIn>
<SignedOut>
<SignInButton />
</SignedOut>
</Layout>
```

Clerk also allows you to protect routes on the server using middleware. Specify which routes are protected, and prompt unauthenticated users to sign in:

```ts title="src/middleware.ts"
import { clerkMiddleware, createRouteMatcher } from '@clerk/astro/server';

const isProtectedRoute = createRouteMatcher([
'/dashboard(.*)',
'/forum(.*)',
]);

export const onRequest = clerkMiddleware((auth, context) => {
if (!auth().userId && isProtectedRoute(context.request)) {
return auth().redirectToSignIn();
}
});
```

### Next Steps

- Read the [official `@clerk/astro` documentation](https://clerk.com/docs/references/astro/overview)
- Start from a template with the [Clerk + Astro Quickstart project](https://github.com/clerk/clerk-astro-quickstart)

## Community Resources

- [Using Microsoft Entra Id EasyAuth with Astro and Azure Static Web App](https://agramont.net/blog/entra-id-easyauth-with-astro/)

0 comments on commit 16a0c44

Please sign in to comment.