web-installer/src/server.js
// This function saves the refresh token to the database
This function saves the refresh token to the database
This function saves the refresh token to the database
async function saveRefreshTokenToDB(username, refreshToken) {
  let client;
  let db;
  try {
    client = await mongoClient.connect(mongoDBUrl, mongo.options);
    db = client.db(dbname);
    const collection = db.collection(instAuthCollection);
    const doc = await collection.findOne({ username });
    if (!doc) {
      await loadAuthenticationToDB();
    }
    const newDoc = { $set: { refreshToken } };
    await collection.updateOne({ username }, newDoc);
  } catch (err) {
    if (err.code === constants.C_442) {
      throw err;
    }
    throw new errors.MongoError(err.message);
  } finally {
    if (client && client.isConnected()) {
      await client.close();
    }
  }
}
async function saveRefreshTokenToDB ( username , refreshToken ) { let client ; let db ; try { client = await mongoClient . connect ( mongoDBUrl , mongo . options ) ; db = client . db ( dbname ) ; const collection = db . collection ( instAuthCollection ) ; const doc = await collection . findOne ( { username } ) ; if ( ! doc ) { await loadAuthenticationToDB ( ) ; } const newDoc = { $set : { refreshToken } } ; await collection . updateOne ( { username } , newDoc ) ; } catch ( err ) { if ( err . code === constants . C_442 ) { throw err ; } throw new errors . MongoError ( err . message ) ; } finally { if ( client && client . isConnected ( ) ) { await client . close ( ) ; } } }
This function saves the refresh token to the database
This function saves the refresh token to the database
// connect // Find authentication document // If we cannot find the authentication document, it must be that the // database was tampered with. Therefore save the .env credentials again // and update with the latest refresh token. // Save the refresh token // If the error is already a mongo error no need to wrap it again as a mongo // error // Check if an error was thrown after a connection was established. If this // is the case close the database connection to prevent leaks