import { ConfigService } from "@nestjs/config" // import { polar } from "@polar-sh/better-auth" // import { Polar } from "@polar-sh/sdk" import { PrismaClient } from "@prisma/client" import { betterAuth } from "better-auth" import { prismaAdapter } from "better-auth/adapters/prisma" import { admin } from "better-auth/plugins" const configService = new ConfigService() const prisma = new PrismaClient() // export const client = new Polar({ // accessToken: configService.get("POLAR_ACCESS_TOKEN")!, // server: configService.get<"sandbox" | "production">("POLAR_SERVER")!, // timeoutMs: 10000, // }) export const auth = betterAuth({ database: prismaAdapter(prisma, { provider: "postgresql", }), emailAndPassword: { enabled: true, requireEmailVerification: true, autoSignIn: true, sendResetPassword: async ({ user, url, token }) => {}, resetPasswordTokenExpiresIn: configService.get("RESET_PASSWORD_TOKEN_EXPIRES_IN")!, }, socialProviders: { google: { clientId: configService.get("GOOGLE_CLIENT_ID")!, clientSecret: configService.get("GOOGLE_CLIENT_SECRET")!, redirectURI: `${configService.get("API_URL")!}/auth/callback/google`!, }, }, trustedOrigins: [configService.get("WEB_URL")!, configService.get("API_URL")!], plugins: [ // polar({ // client, // createCustomerOnSignUp: true, // enableCustomerPortal: true, // checkout: { // enabled: true, // products: [ // { // productId: "prod_P5555555555555555555555", // slug: "pro", // }, // ], // successUrl: configService.get("SUCCESS_URL")!, // }, // webhooks: { // secret: configService.get("POLAR_WEBHOOK_SECRET")!, // onPayload: async payload => { // console.log(payload) // }, // }, // }), admin({}), ], appName: "traxity", baseURL: configService.get("BASE_URL")!, basePath: configService.get("BASE_PATH")!, secret: configService.get("BETTER_AUTH_SECRET")!, emailVerification: { sendVerificationEmail: async (data, request) => {}, sendOnSignUp: true, autoSignInAfterVerification: true, expiresIn: Number(configService.get("EMAIL_VERIFICATION_EXPIRES_IN")!), }, session: { freshAge: Number(configService.get("SESSION_FRESH_AGE")!), expiresIn: Number(configService.get("SESSION_EXPIRES_IN")!), updateAge: Number(configService.get("SESSION_UPDATE_AGE")!), storeSessionInDatabase: true, preserveSessionInDatabase: true, cookieCache: { enabled: true, maxAge: Number(configService.get("SESSION_COOKIE_MAX_AGE")!), }, }, account: { accountLinking: { enabled: true, trustedProviders: ["google", "email-password"], allowDifferentEmails: false, }, }, rateLimit: { enabled: true, window: configService.get("RATE_LIMIT_WINDOW")!, max: configService.get("RATE_LIMIT_MAX")!, customStorage: { get(key) { return Promise.resolve(undefined) }, set(key, value) { return Promise.resolve() }, }, storage: "memory", }, advanced: { ipAddress: { ipAddressHeaders: ["x-client-ip", "x-forwarded-for"], disableIpTracking: false, }, useSecureCookies: configService.get("SECURE_COOKIES") === "true", disableCSRFCheck: false, cookies: { session_token: { name: configService.get("COOKIE_NAME")!, attributes: { httpOnly: true, secure: configService.get("SECURE_COOKIES") === "true", maxAge: Number(configService.get("COOKIE_MAX_AGE")!), expires: new Date( Date.now() + Number(configService.get("COOKIE_MAX_AGE")!) * 1000, ), sameSite: configService.get<"none" | "lax">("COOKIE_SAME_SITE")!, }, }, }, defaultCookieAttributes: { httpOnly: true, expires: new Date(Date.now() + Number(configService.get("COOKIE_MAX_AGE")!) * 1000), maxAge: Number(configService.get("COOKIE_MAX_AGE")!), secure: configService.get("SECURE_COOKIES") === "true", sameSite: configService.get<"none" | "lax">("COOKIE_SAME_SITE")!, }, cookiePrefix: configService.get("COOKIE_PREFIX")!, }, onAPIError: { throw: true, onError(error, ctx) { console.error("Auth API Error: ", error, ctx) }, errorURL: `${configService.get("WEB_URL")!}/auth/error`!, }, logger: { level: "info", log(level, message, ...args) { console.log(level, message, ...args) }, }, })