SaaS Forge uses Better Auth with the Prisma adapter. Users, accounts, sessions and verification tokens live in Postgres (user_schema). The web app, the desktop app and the mobile app all sign in against the same server.
Sign-in methods
| Method | Turn on with | Setup |
|---|---|---|
| Email and password | NEXT_PUBLIC_AUTH_EMAIL=true | Needs an email client for verification: Email |
NEXT_PUBLIC_AUTH_GOOGLE=true | Google OAuth | |
| GitHub | NEXT_PUBLIC_AUTH_GITHUB=true | GitHub OAuth |
NEXT_PUBLIC_AUTH_LINKEDIN=true | LinkedIn OAuth |
The toggles show or hide the buttons, and production refuses to boot when a toggle is on without its credentials. BETTER_AUTH_SECRET (at least 32 characters) is always required.
How the flows work
- Sign-up with email sends a verification email. Sign-in is refused until the address is verified, and verifying signs the user in and lands on
/email-verified. With no email client configured, the link is printed in the server log outside production. - OAuth redirects to the provider and back to
/api/auth/callback/<provider>. Social accounts skip email verification. Google, GitHub and LinkedIn are trusted for account linking, so signing in with a provider that uses the same email attaches it to the existing user. - Password reset:
/forgot-passwordemails a link to/reset-password. - Account settings: from Settings → My Account users update their name and avatar, change their password (or set one after signing up with OAuth), and delete their account. Settings → Sessions lists active sessions and revokes them. Changing email is enabled on the server (confirmed by email) but has no screen yet.
- Rate limits: Better Auth allows 100 requests per minute per client, and 3 email sign-in attempts per minute.
Sessions
- Sessions last 7 days and are refreshed once a day while in use.
- Session data is cached in a signed cookie for 5 minutes, so role changes can take up to 5 minutes to reach an existing session.
- The cookie is named after your product:
<name>.session_token, where<name>isNEXT_PUBLIC_SAAS_NAMEin lowercase with dashes (better-authif unset). In production over HTTPS it gets the__Secure-prefix. - Cookies are
SameSite=None; Secureso the desktop app can use them, and cross-subdomain cookies are enabled in production.
Who becomes admin
The first user created in a database is promoted to admin automatically. Make sure that is you: sign up right after deploying. Admins can change other users' roles in Admin → Users. See Roles & Access Control.
Reading the session
On the server (route handlers, server components, server actions):
import { headers } from "next/headers";
import { auth } from "@workspace/auth/better-auth/auth";
const session = await auth.api.getSession({ headers: await headers() });
if (!session) {
// not signed in
}
session.user.role; // "user" | "admin" | "guest"
In tRPC, use protectedProcedure and read ctx.session. In API routes, call guardRoute(req, "/api/your/route").
In client components:
"use client";
import { authClient, useSession } from "@workspace/auth/better-auth/auth-client";
const { data: session, isPending } = useSession();
await authClient.signIn.social({ provider: "github", callbackURL: "/" });
await authClient.signOut();
Desktop and mobile
- Desktop: the packaged renderer runs on the
app://saas-forgeorigin, which Better Auth and CORS trust explicitly. For OAuth, the provider redirects to/api/auth/desktop-callback, which the Electron main process detects before reloading the app signed in. - Mobile: the Expo plugin (
@better-auth/expo) stores the session in secure storage and handles thesaas-forge://deep-link scheme.
Provider consoles only need the web callback URL. See Desktop App and Mobile App.
Where things live
| File | Purpose |
|---|---|
packages/auth/src/better-auth/auth.ts | Server config: providers, plugins, hooks, sessions, cookies |
packages/auth/src/better-auth/auth-client.ts | React client with the admin plugin |
packages/auth/src/better-auth/cookies.ts | Cookie names derived from NEXT_PUBLIC_SAAS_NAME |
packages/auth/src/better-auth/registration.ts | Invite-only registration rule |
packages/auth/src/better-auth/guestGuard.ts | Account mutations the guest role may not call |
apps/web/app/api/auth/[...all]/route.ts | Better Auth HTTP handler |
apps/web/app/(auth)/ | Sign-in, sign-up, password reset and verification pages |
Adding a provider
Better Auth supports many social providers. To add one, for example Discord:
- Add it to
socialProvidersinauth.ts, and toaccountLinking.trustedProvidersif accounts should link by email. - Add
AUTH_DISCORD_CLIENT_IDandAUTH_DISCORD_CLIENT_SECRETto.env.example, and aNEXT_PUBLIC_AUTH_DISCORDtoggle. - Add the toggle to
INTEGRATION_REQUIREMENTSinapps/web/lib/env.tsand a doctor hint. - Add the button to the sign-in and sign-up pages (and the native apps if you use them), calling
authClient.signIn.social({ provider: "discord" }). - Register
<NEXT_PUBLIC_URL>/api/auth/callback/discordin the provider's console. - Add the provider id to the social check in
emailVerification.sendVerificationEmailso its users skip email verification.
