diff --git a/frameworks/react-cra/add-ons/better-auth/README.md b/frameworks/react-cra/add-ons/better-auth/README.md new file mode 100644 index 00000000..3676c31a --- /dev/null +++ b/frameworks/react-cra/add-ons/better-auth/README.md @@ -0,0 +1,32 @@ +## Setting up Better Auth + +1. Generate and set the `BETTER_AUTH_SECRET` environment variable in your `.env.local`: + + ```bash + npx @better-auth/cli secret + ``` + +2. Visit the [Better Auth documentation](https://www.better-auth.com) to unlock the full potential of authentication in your app. + +### Adding a Database (Optional) + +Better Auth can work in stateless mode, but to persist user data, add a database: + +```typescript +// src/lib/auth.ts +import { betterAuth } from "better-auth"; +import { Pool } from "pg"; + +export const auth = betterAuth({ + database: new Pool({ + connectionString: process.env.DATABASE_URL, + }), + // ... rest of config +}); +``` + +Then run migrations: + +```bash +npx @better-auth/cli migrate +``` diff --git a/frameworks/react-cra/add-ons/better-auth/assets/_dot_env.local.append b/frameworks/react-cra/add-ons/better-auth/assets/_dot_env.local.append new file mode 100644 index 00000000..408cfc28 --- /dev/null +++ b/frameworks/react-cra/add-ons/better-auth/assets/_dot_env.local.append @@ -0,0 +1,3 @@ +# Better Auth configuration +BETTER_AUTH_URL=http://localhost:3000 +BETTER_AUTH_SECRET= # Generate a secret key: `npx @better-auth/cli secret` diff --git a/frameworks/react-cra/add-ons/better-auth/assets/src/integrations/better-auth/header-user.tsx b/frameworks/react-cra/add-ons/better-auth/assets/src/integrations/better-auth/header-user.tsx new file mode 100644 index 00000000..e6f9556f --- /dev/null +++ b/frameworks/react-cra/add-ons/better-auth/assets/src/integrations/better-auth/header-user.tsx @@ -0,0 +1,43 @@ +import { authClient } from "@/lib/auth-client"; +import { Link } from "@tanstack/react-router"; + +export default function BetterAuthHeader() { + const { data: session, isPending } = authClient.useSession(); + + if (isPending) { + return ( +
+ ); + } + + if (session?.user) { + return ( ++ You're signed in as {session.user.email} +
++ {session.user.name} +
++ {session.user.email} +
++ Built with{" "} + + BETTER-AUTH + + . +
++ {isSignUp + ? "Enter your information to create an account" + : "Enter your email below to login to your account"} +
+ + + ++ Built with{" "} + + BETTER-AUTH + + . +
++ {isSignUp() + ? "Enter your information to create an account" + : "Enter your email below to login to your account"} +
+ + + ++ Built with{" "} + + BETTER-AUTH + + . +
++ You're signed in as {user().email} +
+{user().name}
++ {user().email} +
++ Built with{" "} + + BETTER-AUTH + + . +
+