stnd.build

@stnd/account

Circle 2 — Application Superpower (Official Extension) · Stable · First-Party

Role: Authentication, session resolution, magic links, and user account management built on Better-Auth and D1.

When to use: Add user sign-in, session guards, and profile management to an Edge-deployed Standard application.

Quick Start (TL;DR)

import { getSession } from "@stnd/account";

const session = await getSession(Astro.request, Astro.locals);

A comprehensive, self-contained authentication module for the Standard Garden framework, leveraging BetterAuth.

ELI5

Login, sessions, “sign in with Google”, passkeys (Face ID/Touch ID) — all of it, already wired together, so an app doesn’t reinvent auth from scratch. It’s a Standard module: add it to moduleLoad, point it at a D1 database, and the /api/auth/* routes + the sign-in UI just exist.

Install: moduleLoad: ["@stnd/account"] in astro.config.mjs, then follow “Getting Started” below (D1 schema + env vars — there’s real setup, this isn’t zero-config).

Use it (client-side sign-in):

import { signIn } from "@stnd/account/client";
await signIn.social({ provider: "google" });

Features

Getting Started

1. Database Setup

This module requires specific tables in your Cloudflare D1 database. A schema.sql file is provided in this directory.

When you’re ready to test locally or deploy, apply the schema using Wrangler:

wrangler d1 execute <YOUR_DB_NAME> --local --file=packages/account/schema.sql

(Remove --local when applying to your production database).

2. Environment Variables

Ensure your application’s .dev.vars (for local development) and Cloudflare environment secrets contain the following:

BETTER_AUTH_SECRET="your-generated-secret"
GOOGLE_CLIENT_ID="your-google-client-id"
GOOGLE_CLIENT_SECRET="your-google-client-secret"

Note: Generate a secure secret using npx @better-auth/cli secret or openssl rand -base64 32.

3. Application Integration

In your app’s astro.config.js (e.g., apps/ade/astro.config.js), ensure the module is loaded:

import standard from "@stnd/core";

export default defineConfig({
  integrations: [
    standard({
      moduleLoad: [
        "@stnd/account",
        // ... other modules
      ],
      account: {
        // Optional Configuration overrides (defaults fallback to env vars)
        // db: env.DB, // D1 database binding
        // rpName: "My Custom App", // Passkey Relying Party Name
        // rpID: "my-app.com",      // Passkey Relying Party ID
        // baseURL: "https://auth.my-app.com",
        // trustedOrigins: ["https://my-app.com"]
      },
    }),
  ],
});

The sign-in view

views/LoginView.svelte is a complete sign-in for the launcher: an email and the six-digit code sent to it (with a resend that waits), a passkey, and the social providers you list. It sets the user store when it is done and shows a short welcome. Nothing in it belongs to one app: the words, the providers and what happens next are props.

An app registers it under its own ::login (the account module registers none, so two modules never fight over the trigger):

// modules/launcher/index.module.js of an app
{ trigger: "::login", component: "./views/LoginView.svelte", meta: { title: "Connexion" } }
<!-- the app's views/LoginView.svelte -->
<script>
  import LoginView from "@stnd/account/views/LoginView.svelte";
  let { close, back } = $props();
</script>

<LoginView {close} {back} providers={["google"]}
  labels={{ title: "Connexion", provider: "Continuer avec {provider}", sendCode: "Envoyer le code" }} />

labels takes any word in DEFAULT_LABELS ({email}, {seconds} and {provider} are filled in); providers is [] for none; passkey={false} hides the passkey button. When an account needs something after the code (a username, a plan), the app gives needsMore={(user) => …} and a more snippet that calls done(user) when it is finished; onDone replaces the closing of the launcher. Standard Garden’s own view is exactly that: its words, the hand-off for a personal domain, and a ChooseUsername step.

4. Google OAuth Setup

To enable Google Login, you must configure a project in the Google Cloud Console:

  1. Create Project: Start a new project (e.g., Utopie-ADE).
  2. OAuth Consent Screen:
    • Set User Type to External.
    • Add your email as a Test User (required while the app is in “Testing” status).
  3. Credentials:
    • Create OAuth client ID of type Web application.
    • Authorized JavaScript origins: http://localhost:8083 (and your production domain).
    • Authorized redirect URIs: http://localhost:8083/api/auth/callback/google (and https://your-domain.com/api/auth/callback/google).
  4. Copy IDs: Paste the Client ID and Secret into your .env or .dev.vars.
  5. Passkey (WebAuthn) Setup
    Passkeys require a secure context (HTTPS or Localhost).

6. Client Usage

In your Svelte frontend components, you can import the pre-configured better-auth clients:

import { signIn, signUp, useSession, signOut } from "@stnd/account/client";

// Example: sign in with Google
const handleSignIn = async () => {
  await signIn.social({ provider: "google" });
};

Maintenance & Troubleshooting

Database Adapter Errors

If you see Failed to initialize database adapter or db.insertInto is not a function, ensure that the kysely and kysely-d1 dependencies are correctly installed in the module workspace and that the D1Dialect is being passed the valid D1 binding from the request context.

Origin Mismatch (403 Forbidden)

Ensure baseURL in auth.js matches the port your app is actually running on (e.g., 8083 for ADE). BetterAuth’s CSRF protection is strict about port numbers.