From af53ed210972347d0037f2595a8ba068a17834d8 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 15 Apr 2021 16:57:01 +0100 Subject: [PATCH] Fixing some issues with auth module. --- packages/auth/src/db/index.js | 10 +++------- packages/auth/src/index.js | 7 +++---- packages/auth/src/middleware/passport/local.js | 4 ++-- packages/server/src/app.js | 6 ++---- packages/worker/src/index.js | 6 ++---- 5 files changed, 12 insertions(+), 21 deletions(-) diff --git a/packages/auth/src/db/index.js b/packages/auth/src/db/index.js index 2cc033da86..9ae48e68b1 100644 --- a/packages/auth/src/db/index.js +++ b/packages/auth/src/db/index.js @@ -1,9 +1,5 @@ -let Pouch - -module.exports.getDB = () => { - return Pouch -} - module.exports.setDB = pouch => { - Pouch = pouch + module.exports.CouchDB = pouch } + +module.exports.CouchDB = null diff --git a/packages/auth/src/index.js b/packages/auth/src/index.js index ca8b092ec4..705e07f813 100644 --- a/packages/auth/src/index.js +++ b/packages/auth/src/index.js @@ -1,9 +1,8 @@ -const db = require("./db") const passport = require("koa-passport") const LocalStrategy = require("passport-local").Strategy const JwtStrategy = require("passport-jwt").Strategy // const GoogleStrategy = require("passport-google-oauth").Strategy -const CouchDB = require("./db") +const database = require("./db") const { StaticDatabases } = require("./db/utils") const { jwt, local, authenticated } = require("./middleware") const { Cookies, UserStatus } = require("./constants") @@ -29,7 +28,7 @@ passport.use(new JwtStrategy(jwt.options, jwt.authenticate)) passport.serializeUser((user, done) => done(null, user)) passport.deserializeUser(async (user, done) => { - const db = new CouchDB(StaticDatabases.USER.name) + const db = new database.CouchDB(StaticDatabases.USER.name) try { const user = await db.get(user._id) @@ -42,7 +41,7 @@ passport.deserializeUser(async (user, done) => { module.exports = { init(pouch) { - db.setDB(pouch) + database.setDB(pouch) }, passport, Cookies, diff --git a/packages/auth/src/middleware/passport/local.js b/packages/auth/src/middleware/passport/local.js index 886c078e35..78ee2eafff 100644 --- a/packages/auth/src/middleware/passport/local.js +++ b/packages/auth/src/middleware/passport/local.js @@ -1,6 +1,6 @@ const jwt = require("jsonwebtoken") const { UserStatus } = require("../../constants") -const CouchDB = require("../../db") +const database = require("../../db") const { StaticDatabases, generateUserID } = require("../../db/utils") const { compare } = require("../../hashing") const env = require("../../environment") @@ -21,7 +21,7 @@ exports.authenticate = async function(username, password, done) { if (!password) return done(null, false, "Password Required.") // Check the user exists in the instance DB by email - const db = new CouchDB(StaticDatabases.USER.name) + const db = new database.CouchDB(StaticDatabases.USER.name) let dbUser try { diff --git a/packages/server/src/app.js b/packages/server/src/app.js index 7c015e9d26..2f3b977bbe 100644 --- a/packages/server/src/app.js +++ b/packages/server/src/app.js @@ -1,5 +1,7 @@ // need to load environment first const env = require("./environment") +const CouchDB = require("./db") +require("@budibase/auth").init(CouchDB) const Koa = require("koa") const destroyable = require("server-destroy") const electron = require("electron") @@ -11,10 +13,6 @@ const eventEmitter = require("./events") const automations = require("./automations/index") const Sentry = require("@sentry/node") const fileSystem = require("./utilities/fileSystem") -const auth = require("@budibase/auth") -const CouchDB = require("./db") - -auth.init(CouchDB) const app = new Koa() diff --git a/packages/worker/src/index.js b/packages/worker/src/index.js index 225547d337..21ad8381fe 100644 --- a/packages/worker/src/index.js +++ b/packages/worker/src/index.js @@ -1,5 +1,7 @@ // need to load environment first const env = require("./environment") +const CouchDB = require("./db") +require("@budibase/auth").init(CouchDB) const Koa = require("koa") const destroyable = require("server-destroy") const koaBody = require("koa-body") @@ -7,10 +9,6 @@ const { passport } = require("@budibase/auth") const logger = require("koa-pino-logger") const http = require("http") const api = require("./api") -const auth = require("@budibase/auth") -const CouchDB = require("./db") - -auth.init(CouchDB) const app = new Koa()