This is a Next.js project bootstrapped with create-next-app.
pnpm add better-auth pgAdd the following keys to .env.local (defaults shown for local development):
DATABASE_URL=postgres://user:password@localhost:5432/database
GOOGLE_CLIENT_ID=your-google-oauth-client-id
GOOGLE_CLIENT_SECRET=your-google-oauth-client-secret
Create OAuth credentials in the Google Cloud Console and configure the redirect URI <YOUR_DOMAIN>/api/auth/callback/google (e.g., http://localhost:3000/api/auth/callback/google for local development).
src/lib/auth.ts: Better Auth instance using the PostgreSQLpgpool and Google provider.src/app/api/auth/[...betterAuth]/route.ts: Next.js route handler for Better Auth API.src/app/api/auth/session/route.ts:/api/auth/sessionendpoint for session lookups.
src/lib/auth-client.ts: shared Better Auth client for browser usage.src/app/(auth)/sign-in: minimal Google sign-in UI powered by Better Auth.
Use the Better Auth CLI to generate and apply migrations:
pnpm dlx @better-auth/cli@latest generate
pnpm dlx @better-auth/cli@latest migrate- Session cookies are managed through the
nextCookiesplugin. - Google sign-in requests offline access and always prompts the account selector for reliable refresh tokens.