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;
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;
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
- Better Auth Astro Guide
- Better Auth Astro Example
- Better Auth Documentation
- Better Auth GitHub Repository
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.
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
- Read the official
@clerk/astrodocumentation - Start from a template with the Clerk + Astro Quickstart project
Lucia
Lucia is a resource for implementing session-based authentication in a number of frameworks, including Astro.
Guides
- Create a basic sessions API with your chosen database.
- Add session cookies using endpoints and middleware.
- Implement GitHub OAuth using the APIs you implemented.
Examples
- GitHub OAuth example in Astro
- Google OAuth example in Astro
- Email and password example with 2FA in Astro
- Email and password example with 2FA and WebAuthn in Astro
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
- Astro blog tutorial with Scalekit authentication (Authorization Code flow)
- Scalekit developer docs site source (PKCE flow, no SDK)