From 69eddf47db069098216fd557fe739533cbd100a6 Mon Sep 17 00:00:00 2001 From: innoavvlasov Date: Tue, 4 Nov 2025 19:32:58 +0300 Subject: [PATCH] fix auth --- server/routers/procurement/config/db.js | 10 ++++++---- server/routers/procurement/routes/auth.js | 3 +++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/server/routers/procurement/config/db.js b/server/routers/procurement/config/db.js index 601687e..7452f7e 100644 --- a/server/routers/procurement/config/db.js +++ b/server/routers/procurement/config/db.js @@ -39,7 +39,9 @@ const connectWithUri = async (uri, label) => { }); try { - await connection.connection.db.admin().command({ ping: 1 }); + if (connection?.connection?.db) { + await connection.connection.db.admin().command({ ping: 1 }); + } } catch (pingError) { if (isAuthError(pingError)) { await mongoose.connection.close().catch(() => {}); @@ -49,10 +51,10 @@ const connectWithUri = async (uri, label) => { } console.log('✅ MongoDB подключена успешно!'); - console.log(` Хост: ${connection.connection.host}`); - console.log(` БД: ${connection.connection.name}\n`); + console.log(` Хост: ${connection?.connection?.host || 'не указан'}`); + console.log(` БД: ${connection?.connection?.name || 'не указана'}\n`); if (process.env.DEV === 'true') { - console.log(` Пользователь: ${connection.connection.user || 'anonymous'}`); + console.log(` Пользователь: ${connection?.connection?.user || 'anonymous'}`); } return connection; diff --git a/server/routers/procurement/routes/auth.js b/server/routers/procurement/routes/auth.js index 4f64182..aa2943c 100644 --- a/server/routers/procurement/routes/auth.js +++ b/server/routers/procurement/routes/auth.js @@ -117,6 +117,9 @@ const waitForDatabaseConnection = async () => { const verifyAuth = async () => { try { + if (!mongoose.connection.db) { + return false; + } await mongoose.connection.db.admin().command({ listDatabases: 1 }); return true; } catch (error) {