The boilerplate supports social authentication through various providers, currently including Google OAuth. The system is built to be easily extensible for additional providers.
// apps/server/src/lib/auth.ts import { betterAuth } from "better-auth"; import { prismaAdapter } from "better-auth/adapters/prisma"; export const auth = betterAuth({ database: prismaAdapter(prisma, { provider: "postgresql", }), socialProviders: { google: { clientId: process.env.GOOGLE_CLIENT_ID as string, clientSecret: process.env.GOOGLE_CLIENT_SECRET as string, }, // Add more providers here }, trustedOrigins: [ process.env.CORS_ORIGIN as string, ], });
apps/server/.env
# OAuth Providers GOOGLE_CLIENT_ID=your_google_client_id GOOGLE_CLIENT_SECRET=your_google_client_secret # Add more provider credentials as needed
// apps/web/src/lib/auth-client.ts import { createAuthClient } from "better-auth/react"; export const authClient = createAuthClient({ baseURL: process.env.NEXT_PUBLIC_SERVER_URL!, });
// apps/web/src/features/auth/social-buttons.tsx "use client"; import { authClient } from "@/lib/auth-client"; import { Button } from "@/components/ui/button"; export function SocialButtons() { const handleGoogleSignIn = async () => { try { await authClient.signIn.social({ provider: "google", callbackURL: "/dashboard", fetchOptions: { onError: (ctx) => { console.error(ctx.error.message); }, }, }); } }; return ( <div className="space-y-4"> <Button onClick={handleGoogleSignIn} className="w-full" variant="outline" > <svg className="mr-2 h-4 w-4" /> Continue with Google </Button> {/* Add more provider buttons */} </div> ); }
// Server-side export const auth = betterAuth({ socialProviders: { github: { clientId: process.env.GITHUB_CLIENT_ID as string, clientSecret: process.env.GITHUB_CLIENT_SECRET as string, }, }, }); // Client-side const handleGithubSignIn = async () => { await authClient.signInWithProvider("github"); };