HelloWorld
HelloWorld

Reputation: 11249

Reduce Auth Requests

I am making a few node.js scripts using google-api-nodejs-client.

Here is the basic auth request to interact with the api:

const { google } = require("googleapis");
const auth = new google.auth.GoogleAuth({
  keyFile: "credentials.json",
  scopes: "https://www.googleapis.com/auth/spreadsheets",
});
const getAuthClient = async () => {
  try {
    return await auth.getClient();
  } catch (error) {
    console.error(error);
  }
};

const sheetsClient = async () => {
  const client = await getAuthClient();
  return await google.sheets({ version: "v4", auth: client });
};

module.exports = { sheetsClient };

Now, whenever I create a function that needs to use the sheetsClient I need to set it up like this (the examples below are generic examples, I will have other calls to the api where I'll need to get the sheets client. In some cases I'll need to read (get the client) and the write (get the client again) in different functions called one after the other:

const { google } = require("googleapis");
const { sheetsClient } = require("./googleAuth");

const createSheet = async (name) => {
    const client = await sheetsClient();
    const sheet = await client.spreadsheets.create({
        resource: {
            properties: {
                title,
            },
        },
    });
};

const updateSheet = async (name) => {
    const client = await sheetsClient();
    const sheet = await client.spreadsheets.update({
        resource: {
            properties: {
                title,
            },
        },
    });
};

const deleteSheet = async (name) => {
    const client = await sheetsClient();
    const sheet = await client.spreadsheets.delete({
        resource: {
            properties: {
                title,
            },
        },
    });
};

Is there a better way to get access to the client without having to call it everytime within a function?

Upvotes: 0

Views: 62

Answers (1)

Raphael PICCOLO
Raphael PICCOLO

Reputation: 2175

there are many possibilities.

  1. the easiest may be to call this only once, outside of all functions.
const { google } = require("googleapis");
const { sheetsClient } = require("./googleAuth");

// globally defined
const client = ( async () => await sheetsClient())();

// rest of code
const createSheet = async (name) => {

    // deleted : const client = await sheetsClient();

    const sheet = await client.spreadsheets.create({
        resource: {
            properties: {
                title,
            },
        },
    });
};

this will create a global client variable in this js file. then you can remove its declaration from every function.

the code will still run smoothly but there will be only one authentication.

  1. Another way to deal with your problem is to assure that the auth function really is executed only once by using a flag. (this solution is related to memoization)
var client = null;
const getAuthClient = async () => {
  if (client) return client;

  try {
    client = await auth.getClient();
    return client;
  } catch (error) {
    console.error(error);
  }
};

Upvotes: 1

Related Questions