import { socialProviders } from "../../social-provider"; import type { SignInBoxOptions } from "../../store"; export function resolveNextJSFiles(options: SignInBoxOptions) { const files = [ { id: "1", name: "auth.ts", content: `import { betterAuth } from "better-auth"; import { nextCookies } from "better-auth/next";${ options.magicLink ? ` import { magicLink } from "better-auth/plugins";` : "" }${ options.passkey ? ` import { passkey } from "@better-auth/passkey";` : "" } export const auth = betterAuth({ ${ options.email ? `emailAndPassword: { enabled: true, ${ options.requestPasswordReset ? `async sendResetPassword(data, request) { // Send an email to the user with a link to reset their password },` : `` } },` : "" }${ options.socialProviders.length ? `socialProviders: ${JSON.stringify( options.socialProviders.reduce( (acc, provider) => ({ ...acc, [provider]: { clientId: `process.env.${provider.toUpperCase()}_CLIENT_ID!`, clientSecret: `process.env.${provider.toUpperCase()}_CLIENT_SECRET!`, }, }), {}, ), ).replace(/"/g, "")},` : "" } plugins: [${ options.magicLink ? ` magicLink({ async sendMagicLink(data) { // Send an email to the user with a magic link }, }),` : "" }${ options.passkey ? ` passkey(),` : "" } nextCookies(), ], /** if no database is provided, the user data will be stored in memory. * Make sure to provide a database to persist user data **/ }); `, }, { id: "2", name: "auth-client.ts", content: `import { createAuthClient } from "better-auth/react";${ options.magicLink ? ` import { magicLinkClient } from "better-auth/client/plugins";` : "" }${ options.passkey ? ` import { passkeyClient } from "@better-auth/passkey/client";` : "" } export const authClient = createAuthClient({ baseURL: process.env.NEXT_PUBLIC_APP_URL,${ options.magicLink || options.passkey ? ` plugins: [${options.magicLink ? `magicLinkClient()${options.passkey ? "," : ""}` : ""}${ options.passkey ? `passkeyClient()` : "" }],` : "" } }); export const { signIn, signOut, signUp, useSession } = authClient; `, }, { id: "3", name: "sign-in.tsx", content: signInString(options), }, ]; if (options.signUp) { files.push({ id: "4", name: "sign-up.tsx", content: signUpString(options), }); } return files; } const signInString = (options: SignInBoxOptions) => `"use client" import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle, CardDescription, CardFooter } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Checkbox } from "@/components/ui/checkbox"; import { useState } from "react"; import { Loader2, Key } from "lucide-react"; import { signIn } from "@/lib/auth-client"; import Link from "next/link"; import { cn } from "@/lib/utils"; export default function SignIn() {${ options.email || options.magicLink ? ` const [email, setEmail] = useState("");` : "" }${ options.email ? ` const [password, setPassword] = useState("");` : "" } const [loading, setLoading] = useState(false);${ options.rememberMe ? ` const [rememberMe, setRememberMe] = useState(false);` : "" } return ( Sign In Enter your email below to login to your account
${ options.email ? `
{ setEmail(e.target.value); }} value={email} />
${ options.requestPasswordReset ? ` Forgot your password? ` : "" }
setPassword(e.target.value)} />
${ options.rememberMe ? `
{ setRememberMe(!rememberMe); }} />
` : "" }` : "" }${ options.magicLink ? `
{ setEmail(e.target.value); }} value={email} />
` : "" }${ options.email ? ` ` : "" }${ options.passkey ? ` ` : "" }${ options.socialProviders?.length > 0 ? `
3 ? '"justify-between flex-wrap"' : '"justify-between flex-col"' } )}> ${options.socialProviders .map((provider: string) => { const icon = socialProviders[provider as keyof typeof socialProviders] ?.stringIcon || ""; return ``; }) .join("\n\t\t\t\t\t\t")}
` : "" }
${ options.label ? `

built with{" "} better-auth.

` : "" }
); }`; const signUpString = (options: SignInBoxOptions) => `"use client"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { useState } from "react"; import Image from "next/image"; import { Loader2, X } from "lucide-react"; import { signUp } from "@/lib/auth-client"; import { toast } from "sonner"; import { useRouter } from "next/navigation"; export default function SignUp() { const [firstName, setFirstName] = useState(""); const [lastName, setLastName] = useState(""); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [passwordConfirmation, setPasswordConfirmation] = useState(""); const [image, setImage] = useState(null); const [imagePreview, setImagePreview] = useState(null); const router = useRouter(); const [loading, setLoading] = useState(false); const handleImageChange = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (file) { setImage(file); const reader = new FileReader(); reader.onloadend = () => { setImagePreview(reader.result as string); }; reader.readAsDataURL(file); } }; return ( Sign Up Enter your information to create an account
{ setFirstName(e.target.value); }} value={firstName} />
{ setLastName(e.target.value); }} value={lastName} />
{ setEmail(e.target.value); }} value={email} />
setPassword(e.target.value)} autoComplete="new-password" placeholder="Password" />
setPasswordConfirmation(e.target.value)} autoComplete="new-password" placeholder="Confirm Password" />
{imagePreview && (
Profile preview
)}
{imagePreview && ( { setImage(null); setImagePreview(null); }} /> )}
${ options.label ? `

Secured by better-auth.

` : "" }
); } async function convertImageToBase64(file: File): Promise { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.onloadend = () => resolve(reader.result as string); reader.onerror = reject; reader.readAsDataURL(file); }); }`;