Files
supabase/apps/docs/content/guides/auth/quickstarts/astrojs.mdx
Miranda Limonczenko 07e69b9737 docs: fix broken anchor links (#52181)
## Problem

A full docs E2E run with the anchor check from #52174 found 58 broken
anchor links on 53 guide pages. Most point to renamed or removed
headings. Fixing them sets a baseline of zero for the IA restructure.

Closes DOCS-1516

## Solution

- Retarget each link to the section that covers it today, verified on
production.
- Add a custom id where links already use a hand-written slug, such as
`#phx_join`.
- Drop the anchor where no section fits.

## Preview links

Rows with an anchor link straight to a new custom id, which is broken on
live and works on the preview. For the rest, search the page for the
phrase to find the changed link.

| Live | Preview | Search for | Change |
| --- | --- | --- | --- |
|
[with-react](https://supabase.com/docs/guides/getting-started/tutorials/with-react)
|
[with-react](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/getting-started/tutorials/with-react)
| Row Level Security | `auth#row-level-security` →
`database/postgres/row-level-security` (20 links, 15 files) |
| [nextjs](https://supabase.com/docs/guides/auth/quickstarts/nextjs) |
[nextjs](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/auth/quickstarts/nextjs)
| Supabase Auth docs | `auth#authentication` → `auth` |
| [astrojs](https://supabase.com/docs/guides/auth/quickstarts/astrojs) |
[astrojs](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/auth/quickstarts/astrojs)
| Supabase Auth docs | `auth#authentication` → `auth` |
|
[custom-domains](https://supabase.com/docs/guides/platform/custom-domains)
|
[custom-domains](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/platform/custom-domains)
| Owner or Admin permissions | `access-control#manage-team-members` →
`#manage-organization-members` |
|
[network-restrictions](https://supabase.com/docs/guides/platform/network-restrictions)
|
[network-restrictions](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/platform/network-restrictions)
| Owner or Admin permissions | `access-control#manage-team-members` →
`#manage-organization-members` |
|
[postgres-connection-logging](https://supabase.com/docs/guides/platform/postgres-connection-logging)
|
[postgres-connection-logging](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/platform/postgres-connection-logging)
| Owner or Admin permissions | `access-control#manage-team-members` →
`#manage-organization-members` |
|
[ssl-enforcement](https://supabase.com/docs/guides/platform/ssl-enforcement)
|
[ssl-enforcement](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/platform/ssl-enforcement)
| Owner or Admin permissions | `access-control#manage-team-members` →
`#manage-organization-members` |
|
[with-expo-react-native-social-sign-in](https://supabase.com/docs/guides/auth/quickstarts/with-expo-react-native-social-sign-in)
|
[with-expo-react-native-social-sign-in](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/auth/quickstarts/with-expo-react-native-social-sign-in)
| copied earlier | `#get-the-api-keys` → `#get-api-details` |
|
[with-expo-react-native-social-sign-in](https://supabase.com/docs/guides/auth/quickstarts/with-expo-react-native-social-sign-in)
|
[with-expo-react-native-social-sign-in](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/auth/quickstarts/with-expo-react-native-social-sign-in)
| Apple Developer Console | `auth-apple#configuration-web` →
`#configuration-web-oauth` |
|
[with-expo-react-native-social-sign-in](https://supabase.com/docs/guides/auth/quickstarts/with-expo-react-native-social-sign-in)
|
[with-expo-react-native-social-sign-in](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/auth/quickstarts/with-expo-react-native-social-sign-in)
| Google Sign In | Dropped `#react-native`, a non-default tab |
|
[with-flutter](https://supabase.com/docs/guides/getting-started/tutorials/with-flutter)
|
[with-flutter](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/getting-started/tutorials/with-flutter)
| copied earlier | `#get-the-api-keys` → `#get-api-details` |
|
[going-to-prod](https://supabase.com/docs/guides/ai/going-to-prod#hnsw-understanding-efconstruction--efsearch--and-m)
|
[going-to-prod](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/ai/going-to-prod#hnsw-understanding-efconstruction--efsearch--and-m)
| HNSW, understanding | Custom id matching the links from
`ai/choosing-compute-addon` |
|
[rag-with-permissions](https://supabase.com/docs/guides/ai/rag-with-permissions)
|
[rag-with-permissions](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/ai/rag-with-permissions)
| query plan analyzer |
`platform/performance#optimizing-poor-performing-queries` →
`database/query-optimization#analyze-the-query-plan` |
|
[ivf-indexes](https://supabase.com/docs/guides/ai/vector-indexes/ivf-indexes)
|
[ivf-indexes](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/ai/vector-indexes/ivf-indexes)
| Distance operators | `#distance-operators` →
`ai/vector-indexes#distance-operators` |
|
[custom-claims-and-role-based-access-control-rbac](https://supabase.com/docs/guides/api/custom-claims-and-role-based-access-control-rbac)
|
[custom-claims-and-role-based-access-control-rbac](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/api/custom-claims-and-role-based-access-control-rbac)
| local development | `auth-hooks#local-development` → `#developing` |
|
[native-mobile-deep-linking](https://supabase.com/docs/guides/auth/native-mobile-deep-linking)
|
[native-mobile-deep-linking](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/auth/native-mobile-deep-linking)
| password reset |
`passwords#resetting-a-users-password-forgot-password` →
`#resetting-a-password` |
|
[mcp-authentication](https://supabase.com/docs/guides/auth/oauth-server/mcp-authentication)
|
[mcp-authentication](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/auth/oauth-server/mcp-authentication)
| authorization UI | `getting-started#build-your-authorization-endpoint`
→ `#build-your-authorization-ui` |
| [passwords](https://supabase.com/docs/guides/auth/passwords) |
[passwords](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/auth/passwords)
| implicit flow | `sessions#implicit-flow`, `sessions#pkce-flow` →
`sessions/implicit-flow`, `sessions/pkce-flow` |
| [redirect-urls](https://supabase.com/docs/guides/auth/redirect-urls) |
[redirect-urls](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/auth/redirect-urls)
| third-party providers | Dropped the `auth-signinwithoauth` example
anchor |
| [users](https://supabase.com/docs/guides/auth/users) |
[users](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/auth/users)
| User Identity | `auth-identity-linking#the-user-identity` →
`identities#the-user-identity-object` |
|
[upgrading](https://supabase.com/docs/guides/platform/upgrading#upgrading-to-pg_graphql-160)
|
[upgrading](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/platform/upgrading#upgrading-to-pg_graphql-160)
| Upgrading to pg_graphql | Custom id matching the link from
`database/extensions/pg_graphql` |
| [import-data](https://supabase.com/docs/guides/database/import-data) |
[import-data](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/database/import-data)
| disk management | `database-size#disk-management` → `#disk-size` |
| [multigres](https://supabase.com/docs/guides/database/multigres) |
[multigres](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/database/multigres)
| Private Alpha | Dropped the link. `getting-started/features` redirects
to `/features` |
|
[data-deletion](https://supabase.com/docs/guides/database/postgres/data-deletion)
|
[data-deletion](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/database/postgres/data-deletion)
| Check for blocked queries | `#monitoring-locks` →
`#check-for-blocked-queries` |
|
[postgres-log-config](https://supabase.com/docs/guides/database/postgres/postgres-log-config)
|
[postgres-log-config](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/database/postgres/postgres-log-config)
| Supavisor | `connecting-to-postgres#poolers` →
`pooling-and-limits#shared-pooler` and `#dedicated-pooler` |
|
[partner-catalog](https://supabase.com/docs/guides/integrations/partner-catalog)
|
[partner-catalog](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/integrations/partner-catalog)
| Project GraphQL API | `api#graphql-api-overview` → `graphql` |
|
[vercel-marketplace](https://supabase.com/docs/guides/integrations/vercel-marketplace)
|
[vercel-marketplace](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/integrations/vercel-marketplace)
| How your Vercel team maps | `#permissions` →
`#how-your-vercel-team-maps-to-supabase` |
|
[managing-config](https://supabase.com/docs/guides/local-development/managing-config)
|
[managing-config](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/local-development/managing-config)
| Managing secrets for branches |
`branching#managing-secrets-for-branches` →
`branching/configuration#managing-secrets-for-branches` |
| [reports](https://supabase.com/docs/guides/observability/reports) |
[reports](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/observability/reports)
| Query Performance Advisory | `performance#examine-query-performance` →
`#examining-query-performance` |
| [backups](https://supabase.com/docs/guides/platform/backups) |
[backups](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/platform/backups)
| legacy logical backup | `backup-restore#backup-database-using-the-cli`
→ `#back-up-database-using-the-cli` |
|
[backups](https://supabase.com/docs/guides/platform/backups#pitr-restoration-process)
|
[backups](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/platform/backups#pitr-restoration-process)
| Restoration process | Custom id matching the link from
`read-replicas/getting-started` |
|
[branching](https://supabase.com/docs/guides/platform/manage-your-usage/branching)
|
[branching](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/platform/manage-your-usage/branching)
| persistent branches | `branching#persistent-branches` →
`#how-branching-works` |
|
[point-in-time-recovery](https://supabase.com/docs/guides/platform/manage-your-usage/point-in-time-recovery)
|
[point-in-time-recovery](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/platform/manage-your-usage/point-in-time-recovery)
| backup frequency | `backups#frequency-of-backups` → `backups` |
|
[read-replicas](https://supabase.com/docs/guides/platform/read-replicas)
|
[read-replicas](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/platform/read-replicas)
| IPv4 add-on | `ipv4-address#read-replicas` →
`#read-replicas-and-ipv4-add-on` |
|
[protocol](https://supabase.com/docs/guides/realtime/protocol#access-token)
|
[protocol](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/realtime/protocol#access-token)
| access_token | Custom id matching the link from
`realtime/authorization` |
|
[postgres-changes](https://supabase.com/docs/guides/realtime/postgres-changes#negating-a-filter-not)
|
[postgres-changes](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/realtime/postgres-changes#negating-a-filter-not)
| Negating a filter | Custom id matching the existing in-page link |
|
[protocol](https://supabase.com/docs/guides/realtime/protocol#phx_join)
|
[protocol](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/realtime/protocol#phx_join)
| phx_join | Custom id matching the existing in-page link |
| [reports](https://supabase.com/docs/guides/realtime/reports) |
[reports](https://docs-git-docs-fix-broken-anchors-supabase.vercel.app/docs/guides/realtime/reports)
| Broadcast via REST API | `broadcast#broadcast-via-rest-api` →
`#broadcast-using-the-rest-api` |

<details>
<summary>The 15 files with Row Level Security links</summary>

- `_partials/quickstart_intro.mdx`, used by the framework tutorials
- `auth/phone-login`
- `auth/quickstarts/with-expo-react-native-social-sign-in`
- `database/postgres/column-level-security`
- `getting-started/tutorials/`: `with-flutter`, `with-ionic-angular`,
`with-ionic-react`, `with-ionic-vue`, `with-kotlin`, `with-nuxt-3`,
`with-redwoodjs`, `with-solidjs`, `with-svelte`, `with-swift`,
`with-vue-3`

</details>

## Review instructions

1. Open a few live and preview links side by side, and click the changed
link on each to confirm the preview lands on the right section.
2. From a checkout of `docs/e2e-anchor-links`, run the anchor check
against the preview with the changed pages, as described in
`e2e/docs/README.md`:
`DOCS_E2E_PAGE_PATHS=<changed /docs/... paths>
PLAYWRIGHT_BASE_URL=https://docs-git-docs-fix-broken-anchors-supabase.vercel.app
pnpm exec playwright test --grep-invert @a11y`
3. Confirm the anchor check reports no failures.

## Checklist

Check all before review:

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [x] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
2026-10-23 12:24:57 -07:00

665 lines
18 KiB
Plaintext

---
title: 'Use Supabase Auth with Astro'
subtitle: 'Learn how to configure Supabase Auth for Astro with server-side rendering.'
breadcrumb: 'Auth Quickstarts'
hideToc: true
---
## Quickstart
<StepHikeCompact>
<StepHikeCompact.Step step={1}>
<StepHikeCompact.Details title="Create a new Supabase project">
Head over to [database.new](https://database.new) and create a new Supabase project.
Your new database has a table for storing your users. You can see that this table is currently empty by running some SQL in the [SQL Editor](/dashboard/project/_/sql/new).
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```sql name=SQL_EDITOR
select * from auth.users;
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
<StepHikeCompact.Step step={2}>
<StepHikeCompact.Details title="Create an Astro app">
Create a new Astro app using the `npm create` command.
<$Partial path="uiLibCta.mdx" />
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```bash name=Terminal
npm create astro@latest my-app
cd my-app
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
<StepHikeCompact.Step step={3}>
<StepHikeCompact.Details title="Install Supabase libraries and Node adapter">
Install the `@supabase/supabase-js` client library, `@supabase/ssr` for server-side auth, and the `@astrojs/node` adapter to enable server-side rendering.
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```bash name=Terminal
npm install @supabase/supabase-js @supabase/ssr @astrojs/node
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
<StepHikeCompact.Step step={4}>
<StepHikeCompact.Details title="Configure Astro for SSR">
Update your `astro.config.mjs` to enable server-side rendering with the Node adapter.
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```js name=astro.config.mjs
import { defineConfig } from "astro/config";
import node from "@astrojs/node";
export default defineConfig({
output: "server",
adapter: node({
mode: "standalone",
}),
});
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
<StepHikeCompact.Step step={5}>
<StepHikeCompact.Details title="Declare Supabase Environment Variables">
Create a `.env.local` file and populate with your Supabase connection variables:
<ProjectConfigVariables variable="url" />
<ProjectConfigVariables variable="publishable" />
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```dotenv name=.env.local
PUBLIC_SUPABASE_URL=your-project-url
PUBLIC_SUPABASE_PUBLISHABLE_KEY=sb_publishable_key
```
<$Partial path="api_settings.mdx" variables={{ "framework": "astro", "tab": "frameworks" }} />
</StepHikeCompact.Code>
</StepHikeCompact.Step>
<StepHikeCompact.Step step={6}>
<StepHikeCompact.Details title="Create a Supabase client helper">
Create a utility file to initialize the Supabase client with SSR support:
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```ts name=src/lib/supabase.ts
import { createServerClient, parseCookieHeader } from "@supabase/ssr";
import type { AstroCookies } from "astro";
const supabaseUrl = import.meta.env.PUBLIC_SUPABASE_URL
const supabasePublishableKey = import.meta.env.PUBLIC_SUPABASE_PUBLISHABLE_KEY
export function createClient({
request,
cookies,
}: {
request: Request;
cookies: AstroCookies;
}) {
return createServerClient(
supabaseUrl,
supabasePublishableKey,
{
cookies: {
getAll() {
return parseCookieHeader(
request.headers.get("Cookie") ?? ""
);
},
setAll(cookiesToSet) {
cookiesToSet.forEach(({ name, value, options }) =>
cookies.set(name, value, options)
);
},
},
}
);
}
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
<StepHikeCompact.Step step={7}>
<StepHikeCompact.Details title="Create authentication actions">
Create a new file at `src/actions/index.ts` to define server-side authentication actions for signing up, signing in, and signing out:
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```ts name=src/actions/index.ts
import { defineAction } from "astro:actions";
import { z } from "astro/zod";
import { createClient } from "../lib/supabase";
export const server = {
signUp: defineAction({
accept: "form",
input: z.object({
email: z.string().email(),
password: z.string().min(6),
}),
handler: async (input, context) => {
try {
const supabase = createClient({
request: context.request,
cookies: context.cookies,
});
const { error } = await supabase.auth.signUp({
email: input.email,
password: input.password,
options: {
emailRedirectTo: "http://localhost:4321/auth/callback",
},
});
if (error) {
return {
success: false,
message: error.message,
};
}
return {
success: true,
message: "Check your email to confirm your account",
};
} catch (err) {
return {
success: false,
message: "Unexpected error",
};
}
},
}),
signIn: defineAction({
accept: "form",
input: z.object({
email: z.string().email(),
password: z.string(),
}),
handler: async (input, context) => {
try {
const supabase = createClient({
request: context.request,
cookies: context.cookies,
});
const { error } = await supabase.auth.signInWithPassword({
email: input.email,
password: input.password,
});
if (error) {
return {
success: false,
message: error.message,
};
}
return {
success: true,
message: "Signed in successfully",
};
} catch (err) {
return {
success: false,
message: "Unexpected error",
};
}
},
}),
signOut: defineAction({
handler: async (_, context) => {
try {
const supabase = createClient({
request: context.request,
cookies: context.cookies,
});
await supabase.auth.signOut();
return {
success: true,
};
} catch (err) {
return {
success: false,
message: "Failed to sign out",
};
}
},
}),
};
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
<StepHikeCompact.Step step={8}>
<StepHikeCompact.Details title="Customize email template">
Before users can confirm their email, update the Supabase email template to send the token hash to your callback URL.
In your [Supabase project dashboard](/dashboard/project/_/auth/templates):
- Go to **Auth** > **Email Templates**
- Select the **Confirm signup** template
- Change `{{ .ConfirmationURL }}` to `{{ .SiteURL }}/auth/callback?token_hash={{ .TokenHash }}&type=email`.
- Change your [Site URL](/dashboard/project/_/auth/url-configuration) to `http://localhost:4321`
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```html name=Email\ Template
{{ .SiteURL }}/auth/callback?token_hash={{ .TokenHash }}&type=email
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
<StepHikeCompact.Step step={9}>
<StepHikeCompact.Details title="Create an auth callback page">
Create a new file at `src/pages/auth/callback.astro` to handle the email confirmation callback. Extract the token from the URL and verify it with Supabase:
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```astro name=src/pages/auth/callback.astro
---
import { createClient } from "../../lib/supabase";
import type { EmailOtpType } from "@supabase/supabase-js";
const supabase = createClient({
request: Astro.request,
cookies: Astro.cookies,
});
const requestUrl = new URL(Astro.request.url);
const token_hash = requestUrl.searchParams.get('token_hash');
const type = requestUrl.searchParams.get('type') as EmailOtpType | null;
if (token_hash && type) {
const { error } = await supabase.auth.verifyOtp({
token_hash,
type,
});
if (!error) {
return Astro.redirect("/dashboard");
}
}
return Astro.redirect("/auth/signin");
---
<html>
<head>
<title>Email Confirmation</title>
</head>
<body>
<p>Confirming your email...</p>
</body>
</html>
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
<StepHikeCompact.Step step={10}>
<StepHikeCompact.Details title="Create a sign-up page">
Create a new file at `src/pages/auth/signup.astro` with a sign-up form. Use a client-side event listener to handle form submission:
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```astro name=src/pages/auth/signup.astro
---
import { createClient } from "../../lib/supabase";
const supabase = createClient({
request: Astro.request,
cookies: Astro.cookies,
});
const { data } = await supabase.auth.getUser();
if (data?.user) {
return Astro.redirect("/dashboard");
}
---
<html>
<head>
<title>Sign Up</title>
</head>
<body>
<h1>Sign Up</h1>
<div id="message"></div>
<form id="signup-form">
<div>
<label for="email">Email</label>
<input
id="email"
type="email"
name="email"
placeholder="your@email.com"
required
/>
</div>
<div>
<label for="password">Password</label>
<input
id="password"
type="password"
name="password"
placeholder="At least 6 characters"
required
/>
</div>
<button type="submit" id="signup-btn">Sign Up</button>
</form>
<p>
Already have an account? <a href="/auth/signin">Sign in</a>
</p>
</body>
</html>
<script>
import { actions } from "astro:actions";
const form = document.querySelector("#signup-form") as HTMLFormElement;
const btn = document.getElementById("signup-btn") as HTMLButtonElement;
const messageEl = document.getElementById("message") as HTMLDivElement;
form?.addEventListener("submit", async (e) => {
e.preventDefault();
btn.disabled = true;
btn.textContent = "Signing up...";
messageEl.textContent = "";
try {
const formData = new FormData(form);
const result = await actions.signUp(formData);
if (!result.data?.success) {
btn.disabled = false;
btn.textContent = "Sign Up";
messageEl.textContent = result.data?.message || "Sign up failed";
messageEl.style.color = "red";
return;
}
messageEl.textContent = result.data.message;
messageEl.style.color = "green";
btn.textContent = "Sign Up";
} catch (error) {
btn.disabled = false;
btn.textContent = "Sign Up";
messageEl.textContent = "An error occurred. Please try again.";
messageEl.style.color = "red";
console.error(error);
}
});
</script>
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
<StepHikeCompact.Step step={11}>
<StepHikeCompact.Details title="Create a sign-in page">
Create a new file at `src/pages/auth/signin.astro` with a sign-in form. Use a client-side event listener to handle form submission:
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```astro name=src/pages/auth/signin.astro
---
import { createClient } from "../../lib/supabase";
const supabase = createClient({
request: Astro.request,
cookies: Astro.cookies,
});
const { data } = await supabase.auth.getUser();
if (data?.user) {
return Astro.redirect("/dashboard");
}
---
<html>
<head>
<title>Sign In</title>
</head>
<body>
<h1>Sign In</h1>
<div id="message"></div>
<form id="signin-form">
<div>
<label for="email">Email</label>
<input
id="email"
type="email"
name="email"
placeholder="your@email.com"
required
/>
</div>
<div>
<label for="password">Password</label>
<input
id="password"
type="password"
name="password"
placeholder="Your password"
required
/>
</div>
<button type="submit" id="signin-btn">Sign In</button>
</form>
<p>
Don't have an account? <a href="/auth/signup">Sign up</a>
</p>
</body>
</html>
<script>
import { actions } from "astro:actions";
const form = document.querySelector("#signin-form") as HTMLFormElement;
const btn = document.getElementById("signin-btn") as HTMLButtonElement;
const messageEl = document.getElementById("message") as HTMLDivElement;
form?.addEventListener("submit", async (e) => {
e.preventDefault();
btn.disabled = true;
btn.textContent = "Signing in...";
messageEl.textContent = "";
try {
const formData = new FormData(form);
const result = await actions.signIn(formData);
if (!result.data?.success) {
btn.disabled = false;
btn.textContent = "Sign In";
messageEl.textContent = result.data?.message || "Sign in failed";
messageEl.style.color = "red";
return;
}
// Redirect to dashboard on successful sign in
window.location.href = "/dashboard";
} catch (error) {
btn.disabled = false;
btn.textContent = "Sign In";
messageEl.textContent = "An error occurred. Please try again.";
messageEl.style.color = "red";
console.error(error);
}
});
</script>
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
<StepHikeCompact.Step step={12}>
<StepHikeCompact.Details title="Create a dashboard page">
Create a new file at `src/pages/dashboard.astro` to display the authenticated user's information. Use a client-side event listener for the sign-out button:
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```astro name=src/pages/dashboard.astro
---
import { createClient } from "../lib/supabase";
const supabase = createClient({
request: Astro.request,
cookies: Astro.cookies,
});
const { data } = await supabase.auth.getUser();
const user = data?.user;
if (!user) {
return Astro.redirect("/auth/signin");
}
---
<html>
<head>
<title>Dashboard</title>
</head>
<body>
<h1>Welcome!</h1>
<p>Email: {user.email}</p>
<p>User ID: {user.id}</p>
<button id="signout-btn">Sign Out</button>
</body>
</html>
<script>
import { actions } from "astro:actions";
const btn = document.getElementById("signout-btn") as HTMLButtonElement;
btn?.addEventListener("click", async (e) => {
e.preventDefault();
btn.disabled = true;
btn.textContent = "Signing out...";
try {
const result = await actions.signOut();
if (!result.data?.success) {
btn.disabled = false;
btn.textContent = "Sign Out";
alert("Failed to sign out");
return;
}
// Redirect to signin page
window.location.href = "/auth/signin";
} catch (error) {
btn.disabled = false;
btn.textContent = "Sign Out";
console.error(error);
}
});
</script>
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
<StepHikeCompact.Step step={13}>
<StepHikeCompact.Details title="Start the app">
Start the development server, then navigate to http://localhost:4321/auth/signup to test the authentication.
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```bash name=Terminal
npm run dev
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
</StepHikeCompact>
## Learn more
- [Supabase Auth docs](/docs/guides/auth) for more Supabase authentication methods