Welcome {session.user?.name}
) : (Not logged in
) }{session.user?.name}
``` 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. src/middleware.ts ```ts import { auth } from "../../../auth"; // import your Better Auth instance import { defineMiddleware } from "astro:middleware"; export const onRequest = defineMiddleware(async (context, next) => { const isAuthed = await auth.api .getSession({ headers: context.request.headers, }) if (context.url.pathname === "/dashboard" && !isAuthed) { return context.redirect("/"); } return next(); }); ``` ### Next Steps [Section titled “Next Steps”](#next-steps-1) * [Better Auth Astro Guide](https://www.better-auth.com/docs/integrations/astro) * [Better Auth Astro Example](https://github.com/better-auth/examples/tree/main/astro-example) * [Better Auth Documentation](https://www.better-auth.com/docs) * [Better Auth GitHub Repository](https://github.com/better-auth/better-auth) ## Clerk [Section titled “Clerk”](#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 [Section titled “Installation”](#installation-2) Install `@clerk/astro` using the package manager of your choice. * npm ```shell npm install @clerk/astro ``` * pnpm ```shell pnpm add @clerk/astro ``` * Yarn ```shell yarn add @clerk/astro ``` ### Configuration [Section titled “Configuration”](#configuration-2) 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 [Section titled “Usage”](#usage-2) 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: src/pages/index.astro ```astro --- import Layout from 'src/layouts/Base.astro'; import { SignedIn, SignedOut, UserButton, SignInButton } from '@clerk/astro/components'; export const prerender = false; // Not needed in 'server' mode ---