41 lines
1.0 KiB
TypeScript
41 lines
1.0 KiB
TypeScript
import { betterAuth } from "better-auth";
|
|
import {mongodbAdapter} from "better-auth/adapters/mongodb";
|
|
import {connectToDatabase} from "@/database/mongoose";
|
|
import {nextCookies} from "better-auth/next-js";
|
|
|
|
|
|
let authInstance: ReturnType<typeof betterAuth> | null = null;
|
|
|
|
|
|
export const getAuth = async () => {
|
|
if(authInstance) {
|
|
return authInstance;
|
|
}
|
|
|
|
const mongoose = await connectToDatabase();
|
|
const db = mongoose.connection;
|
|
|
|
if (!db) {
|
|
throw new Error("MongoDB connection not found!");
|
|
}
|
|
|
|
authInstance = betterAuth({
|
|
database: mongodbAdapter(db as any),
|
|
secret: process.env.BETTER_AUTH_SECRET,
|
|
baseURL: process.env.BETTER_AUTH_URL,
|
|
emailAndPassword: {
|
|
enabled: true,
|
|
disableSignUp: false,
|
|
requireEmailVerification: false,
|
|
minPasswordLength: 8,
|
|
maxPasswordLength: 128,
|
|
autoSignIn: true,
|
|
},
|
|
plugins: [nextCookies()],
|
|
|
|
});
|
|
|
|
return authInstance;
|
|
}
|
|
|
|
export const auth = await getAuth(); |