Skip to content

OpenStock is an open-source alternative to expensive market platforms. Track real-time prices, set personalized alerts, and explore detailed company insights — built openly, for everyone, forever free.

fix(auth): sign out other sessions on password reset, refuse to run without a secret

ravixalgorithm02d5aaa5f02b
diff --git a/lib/better-auth/auth.ts b/lib/better-auth/auth.ts
index a6d6ba3..5c0082b 100644
--- a/lib/better-auth/auth.ts
+++ b/lib/better-auth/auth.ts
@@ -30,6 +30,8 @@ const createAuth = (database: MongoDb) => betterAuth({
minPasswordLength: 8,
maxPasswordLength: 128,
autoSignIn: true,
+ // A reset is how a victim takes an account back: sign out every other session (off by default)
+ revokeSessionsOnPasswordReset: true,
sendResetPassword: async ({ user, url }) => {
void sendPasswordResetEmail({
email: user.email,
@@ -73,6 +75,12 @@ let authInstance: ReturnType<typeof createAuth> | null = null;
export const getAuth = async () => {
if (authInstance) return authInstance;
+ // Without a secret better-auth only logs an error and signs cookies with a public default, so anyone could forge
+ // a session. Checked here (first use), not at import, so `next build` still runs without secrets.
+ if (process.env.NODE_ENV === 'production' && !process.env.BETTER_AUTH_SECRET && !process.env.AUTH_SECRET) {
+ throw new Error('BETTER_AUTH_SECRET must be set in production');
+ }
+
const mongoose = await connectToDatabase();
const database = mongoose.connection.db;
if (!database) {