Files
kolaytercih/src/lib/auth.ts
bilalgursen e62c6f07df
Some checks failed
Deploy / deploy (push) Failing after 1h28m6s
Update package.json scripts, enhance layout with new components, and improve API error handling
- Added new scripts for database operations and temporary production in package.json.
- Integrated KaydetBannerLazy component into the layout for improved user notifications.
- Enhanced API error handling in the soru route to mark credit exhaustion.
- Updated the IletisimPage for better button styling and user experience.
- Refactored ListemPage to streamline user flow and improve session handling.
- Removed unused ListePaneli component to clean up the codebase.
2026-08-06 02:27:42 +03:00

80 lines
2.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { magicLink } from "better-auth/plugins";
import { appDb, schema } from "./appdb";
import { grantCredits, DENEME_KREDISI } from "./credits";
import { magicLinkEpostasi } from "./eposta";
import { epostaGonder } from "./eposta-gonder";
async function sendMagicLinkEmail(email: string, url: string) {
if (process.env.NODE_ENV !== "production") {
// Dev paneli "anında giriş" için son üretilen linki yakalar (bkz.
// src/lib/dev/actions.ts) — prod'da asla çalışmaz.
(globalThis as { __sonMagicLink?: string }).__sonMagicLink = url;
}
if (!process.env.RESEND_API_KEY) {
// Dev fallback: Resend anahtarı yoksa linki terminale yaz
console.log(`\n[giris] Magic link for ${email}:\n${url}\n`);
return;
}
// Gönderim hatası better-auth'a fırlatılır — aksi halde kullanıcı
// "gönderildi" ekranını görür ama mail hiç çıkmaz.
await epostaGonder(email, magicLinkEpostasi(url));
}
export const auth = betterAuth({
baseURL: process.env.BETTER_AUTH_URL ?? "http://localhost:3000",
secret: process.env.BETTER_AUTH_SECRET,
database: drizzleAdapter(appDb, {
provider: "sqlite",
schema: {
user: schema.user,
session: schema.session,
account: schema.account,
verification: schema.verification,
},
}),
session: {
// Session'ı imzalı cookie'de önbelle: her istekte session tablosu
// sorgusu yapılmaz. Kredi/paket tazeliği getCurrentUser()'daki ayrı
// user sorgusuyla korunur (src/lib/session.ts).
cookieCache: { enabled: true, maxAge: 5 * 60 },
},
user: {
additionalFields: {
creditBalance: { type: "number", defaultValue: 0, input: false },
hasPaket: { type: "boolean", defaultValue: false, input: false },
},
},
socialProviders: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID ?? "",
clientSecret: process.env.GOOGLE_CLIENT_SECRET ?? "",
},
},
plugins: [
magicLink({
sendMagicLink: async ({ email, url }) => {
await sendMagicLinkEmail(email, url);
},
}),
],
databaseHooks: {
user: {
create: {
after: async (newUser) => {
// refId = userId → UNIQUE(reason, refId) ile tek seferlik
await grantCredits({
userId: newUser.id,
delta: DENEME_KREDISI,
reason: "trial_grant",
refId: newUser.id,
});
},
},
},
},
});
export type Session = typeof auth.$Infer.Session;