Skip to content

astro

Authentication

Astro

Authentication

Authentication

};


Follow the [Better Auth Astro Guide](https://www.better-auth.com/docs/integrations/astro) to learn more.

### Usage

Better Auth offers a `createAuthClient` helper for various frameworks, including Vanilla JS, React, Vue, Svelte, and Solid. 

For example, to create a client for React, import the helper from `'better-auth/react'`:



<UIFrameworkTabs>
  <Fragment slot="react">
   ```ts title="src/lib/auth-client.ts"
  import { createAuthClient } from 'better-auth/react';

  export const authClient = createAuthClient();

  export const { signIn, signOut } = authClient;
```ts title="src/lib/auth-client.ts" import { createAuthClient } from 'better-auth/solid';

export const authClient = createAuthClient();

export const { signIn, signOut } = authClient;

</Fragment>
<Fragment slot="svelte">
 ```ts title="src/lib/auth-client.ts"
import { createAuthClient } from 'better-auth/svelte';

export const authClient = createAuthClient();

export const { signIn, signOut } = authClient;
```ts title="src/lib/auth-client.ts" import { createAuthClient } from 'better-auth/vue';

export const authClient = createAuthClient();

export const { signIn, signOut } = authClient;

</Fragment>
</UIFrameworkTabs>

Once your client is set up, you can use it to authenticate users in your Astro components or any framework-specific files. The following example adds the ability to log in or log out with your configured `signIn()` and `signOut()` functions.


```astro title="src/pages/index.astro"
---

---
<Layout>
<button id="login">Login</button>
<button id="logout">Logout</button>

<script>
  const { signIn, signOut } = await import("./lib/auth-client")
  document.querySelector("#login").onclick = () => signIn.social({
    provider: "github",
    callbackURL: "/dashboard",
  })
  document.querySelector("#logout").onclick = () => signOut()
</script>
</Layout>

You can then use the auth object to get the user's session data in your server-side code. The following example personalizes page content by displaying an authenticated user's name:

---

---

<p>{session.user?.name}</p>

You can also use the auth object to protect your routes using middleware. The following example checks whether a user trying to access a logged-in dashboard route is authenticated, and redirects them to the home page if not.



	}
	return next();
});

Next Steps

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 is available.

Installation

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

```shell npm install @clerk/astro ``` ```shell pnpm add @clerk/astro ``` ```shell yarn add @clerk/astro ```

Configuration

Follow Clerk's own Astro Quickstart guide 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:

---




  }
});

Next Steps

Lucia

Lucia is a resource for implementing session-based authentication in a number of frameworks, including Astro.

Guides

  1. Create a basic sessions API with your chosen database.
  2. Add session cookies using endpoints and middleware.
  3. Implement GitHub OAuth using the APIs you implemented.

Examples

Scalekit

Scalekit is an authentication platform built for B2B and AI applications. It provides social login, enterprise SSO, magic links, and more — managing the full OAuth 2.0 / OIDC flow so you get back tokens and a user profile without building any login UI. A single Scalekit environment supports multiple applications, so users authenticate once and share the same session across all your properties (for example, app.yourcompany.com and docs.yourcompany.com).

Guide

Follow the Scalekit & Astro guide to add authentication to your Astro SSR project using social login, enterprise SSO, and more.

Examples

Community Resources