Skip to content

Authentication

Dashild does not ship an auth library. The panel needs two things from the app:

export const panel = definePanel({
// ...
auth: {
getCurrentUser,
signOut, // optional
},
});

getCurrentUser runs on the server and returns the signed-in user or null. signOut is optional; when present, the user menu shows a sign-out action.

Terminal window
pnpm dashild init --auth better-auth --yes

init creates the generated auth files and connects the app to Better Auth. --auth better-auth selects that integration; --yes applies the plan without asking for confirmation. Review the generated files and environment variables before using the app.

This writes:

src/lib/auth.ts
src/lib/auth-client.ts
src/lib/session.ts
src/lib/auth-actions.ts
src/lib/email.ts
src/app/api/auth/[...all]/route.ts
src/app/login/page.tsx
src/app/signup/page.tsx
src/app/forgot-password/page.tsx
src/app/reset-password/page.tsx
src/components/dashild/login-form.tsx
src/components/dashild/signup-form.tsx
src/components/dashild/forgot-password-form.tsx
src/components/dashild/reset-password-form.tsx

The folders are conditional: if the app already has lib/ and components/ at the root instead of under src/, the files go there. The app directory follows the same rule.

It also adds better-auth to package.json.

If the schema has none of User, Session, Account, or Verification, init appends all four. If it finds any of them, it adds none and warns; adapt the existing models yourself. The check is all-or-nothing.

create-dashild writes .env and a committed .env.example. dashild init only appends missing keys to .env.example; it never writes .env.

Variable Where Purpose
DATABASE_URL .env and .env.example App and Prisma connection.
DIRECT_URL .env when given; commented placeholder in .env.example Non-pooled URL for migrations.
BETTER_AUTH_SECRET With Better Auth Session signing. Generate with openssl rand -base64 32.
BETTER_AUTH_URL With Better Auth Public origin; must match the browser URL.
ALLOW_SIGNUP With Better Auth false closes public sign-up.
REQUIRE_EMAIL_VERIFICATION With Better Auth true requires the verification link.
RESEND_API_KEY .env.example only, commented (Better Auth) If set, sendEmail sends through Resend.
EMAIL_FROM .env.example only, commented (Better Auth) Resend sender.

The email keys are never written to .env: uncomment them there (or add them to your deployment environment) when you configure a provider.

BETTER_AUTH_URL must match the origin you open in the browser. A mismatch makes sign-in fail with an origin error.

  • ALLOW_SIGNUP=false disables the sign-up API and the signup page shows “Sign-up disabled”. Create users from your own admin flow.
  • REQUIRE_EMAIL_VERIFICATION=true sends the verification link on sign-up and blocks sign-in until the email is verified.
  • /forgot-password requests a reset link; /reset-password?token=... sets the new password.
  • Rate limiting is enabled (100 requests per minute) in the generated config.

src/lib/email.ts uses Resend when RESEND_API_KEY is present:

await fetch("https://api.resend.com/emails", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.RESEND_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
from: process.env.EMAIL_FROM ?? "Dashild <onboarding@resend.dev>",
to,
subject,
text,
}),
});

Without a key it logs the email in development and throws in production, so password reset and verification never lose emails silently. Replace it with Postmark, SES, or your own transport if you prefer.

Use the default mode and fill the adapter:

Terminal window
pnpm dashild init --auth none --yes

Here --auth none tells Dashild not to generate Better Auth. It still creates the panel integration; you then connect your existing authentication system by implementing getCurrentUser.

async function getCurrentUser(): Promise<CurrentUser | null> {
const session = await myAuth.getSession();
if (!session) return null;
return {
id: session.user.id,
email: session.user.email,
name: session.user.name,
organizationId: session.user.organizationId,
roles: session.user.roles,
};
}

CurrentUser requires id; email, name, organizationId, and roles are optional. Returning organizationId and roles is enough for policies. For tenant isolation end to end, see Multi-tenancy.

If the schema already has any Better Auth model, init --auth better-auth does not modify the schema; it warns and leaves the adaptation to you.