web-installer/src/server.js
// user would need to login each time the server restarts)
user would need to login each time the server restarts)
user would need to login each time the server restarts )
async function loadAuthenticationToDB() {
  let client;
  let db;
  try {
    client = await mongoClient.connect(mongoDBUrl, mongo.options);
    db = client.db(dbname);
    const collection = db.collection(instAuthCollection);
    const username = installerCredentials.INSTALLER_USERNAME;
    const hashedPass = bcrypt.hashSync(installerCredentials.INSTALLER_PASSWORD, 10);
    const password = installerCredentials.INSTALLER_PASSWORD;
    const authDoc = { username, password: hashedPass, refreshToken: '' };
    const doc = await collection.findOne({ username });
    if (!doc) {
      await collection.insertOne(authDoc);
      return;
    }
    if (!bcrypt.compareSync(password, doc.password)) {
      const newDoc = { $set: authDoc };
      await collection.updateOne({ username }, newDoc);
    }
  } catch (err) {
    throw new errors.MongoError(err.message);
  } finally {
    if (client && client.isConnected()) {
      await client.close();
    }
  }
}
async function loadAuthenticationToDB ( ) { let client ; let db ; try { client = await mongoClient . connect ( mongoDBUrl , mongo . options ) ; db = client . db ( dbname ) ; const collection = db . collection ( instAuthCollection ) ; const username = installerCredentials . INSTALLER_USERNAME ; const hashedPass = bcrypt . hashSync ( installerCredentials . INSTALLER_PASSWORD , 10 ) ; const password = installerCredentials . INSTALLER_PASSWORD ; const authDoc = { username , password : hashedPass , refreshToken : '' } ; const doc = await collection . findOne ( { username } ) ; if ( ! doc ) { await collection . insertOne ( authDoc ) ; return ; } if ( ! bcrypt . compareSync ( password , doc . password ) ) { const newDoc = { $set : authDoc } ; await collection . updateOne ( { username } , newDoc ) ; } } catch ( err ) { throw new errors . MongoError ( err . message ) ; } finally { if ( client && client . isConnected ( ) ) { await client . close ( ) ; } } }
user would need to login each time the server restarts)
user would need to login each time the server restarts )
// connect // Find authentication document // If the credentials inside the .env are not found insert the credentials // in the database. // If the credentials are found and the password changed update the record. // We must also invalidate the refresh token so that eventually all logged // in users are logged out when the access token expires. // If an error is raised throw a MongoError // Check if an error was thrown after a connection was established. If this // is the case close the database connection to prevent leaks