Using middleware to interact with BigQuery API Client and accessing secrets.json files

I am exploring the idea of utilizing a secrets folder to securely store my BigQuery API client secrets JSON file and leveraging the BigQuery client library for Node.js to execute queries on the database. However, the documentation seems to focus on loading credentials from a JSON file indirectly rather than directly.

As I am working with TypeScript to query a table and add rows, I find myself a bit perplexed when it comes to accessing the path of a secrets folder that needs to be kept confidential and not accessible to the client side, especially within my middleware setup in NEXT.js.

Below is the code snippet for reference:

import type { NextApiHandler } from 'next';
import axios from 'axios';
import { BigQuery } from '@google-cloud/bigquery';
import bqSecrets from '../../../bigquery/keys.json';
const options = {
  keyFilename: '../../../secrets/keys.json',
  projectId: 'XXXXXXXXXXXXXXXXXXXXXX',
};

const bigquery = new BigQuery(options);

const submitUserData: NextApiHandler = async (request, response) => {
  const { geoData, googleData, post, userRole } = request.body;
  if (!post) response.json({ error: false, msg: 'Not Required' });
  else {
    delete googleData.isAuthenticated;

    

    try {
      const rep = await bigquery
        .dataset('stackconnect')
        .table('googleoAuth')
        .insert([requestPayload]);
      console.log(await rep);
      response.json({ error: false, msg: 'Success' });
    } catch (e) {
      console.log('Error = ', e);
      response.json({ error: true, msg: 'Error' });
    }
  }
};

However, this results in an error stating: File not Found. Can anyone assist me in understanding how to either locate the file in NEXT.js or how to directly pass the JSON data using the BigQuery Node Client?

Furthermore, I am curious to know where the most suitable place in NEXT.js would be to securely store sensitive information that is not supposed to be exposed to the client side.

Answer №1

Upon thorough examination of the source code, I discovered an attribute named 'credentials', which prompted me to make the following modification in order to successfully execute the task:

import bqSecrets from '../../../bigquery/keys.json';
const options = {
  credentials: bqSecrets,
  projectId: 'project_id',
};

const bigquery = new BigQuery(options);

This solution should prove helpful to individuals seeking to directly integrate JSON credentials into the BigQuery Node Client.

In addition, I am still uncertain about the file structure of NEXT, particularly whether assets other than those located within the public folder are accessible on the client side.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing Angular to make API requests and handle promises

Currently, I am facing a challenge in my angular application which involves working with the soundcloud api. The issue arises when I make a call to the soundcloud api to retrieve my tracks, loop through them to extract the iframe embed object, inject it in ...

How can I modify a dynamically generated table to include rowspan and colspan attributes in the rows?

My table was automatically created using data from the database. var rows = ""; rows += "<tr class='row_primary'>"; rows += "<td>COL 1</td>"; rows += "<td>COL 2</td>"; rows += "<td> ...

Reorganize code in JavaScript and TypeScript files using VSCode

Is there a way to efficiently organize the code within a .js / .ts file using Vscode? Specifically, when working inside a Class, my goal is to have static variables at the top, followed by variables, then methods, and so on automatically. I did some resea ...

Which approach is more impactful: consistently sending an ajax request or breaking down the result within a function?

My JSON data consists of news entries with titles, text, and dates. Here is an example: { "news": [ {"title": "some title #1","text": "text","date": "27.12.15 23:45"}, {"title": "some title #2","text": "text","date": "26.12.15 22:35"}, ... ...

What are some solutions for resolving the issue of a neutralino white screen?

After setting up a new neutralino js application, I encountered an issue where upon running it with the 'neu run' command, all I see is a blank white screen. I tried using the CheckNetIsolation.exe ... command to troubleshoot the problem, but unf ...

Verify whether the element in the DOM is a checkbox

What is the method to determine whether a specific DOM element is a checkbox? Situation: In a collection of dynamically assigned textboxes and checkboxes, I am unable to differentiate between them based on their type. ...

Tips for setting a jQuery variable equal to the value of a JSON object

When I try to assign courseid and batchid as defaults using defaultValue => defaultValue: courseid and defaultValue: batchid, the values are not being saved correctly in my database. $(document).ready(function() { var courseid = null; var bat ...

Incorporate a CSS transition duration for switching classes with JavaScript

I need assistance with adding a transition time to my JavaScript toggle class function. I had previously coded it but have now forgotten how to do so. Any guidance would be highly appreciated. <!DOCTYPE html> <html> <head> <style typ ...

Jest is having trouble recognizing a custom global function during testing, even though it functions properly outside of testing

In my Express app, I have a custom function called foo that is globally scoped. However, when running Jest test scripts, the function is being recognized as undefined, causing any tests that rely on it to fail. This is declared in index.d.ts: declare glob ...

Button Click Not Allowing Webpage Scroll

I am currently in the process of developing a website that aims to incorporate a significant amount of motion and interactivity. The concept I have devised involves a scenario where clicking on a button from the "main menu" will trigger a horizontal and ve ...

Utilize a class method within the .map function in ReactJS

In my ReactJS file below: import React, { Component } from "react"; import Topic from "./Topic"; import $ from "jquery"; import { library } from '@fortawesome/fontawesome-svg-core' import { FontAwesomeIcon } from '@fortawesome/react-fontaw ...

Internet Explorer's support for the `<summary>` tag in HTML

Is there a method to enable the summary tag in Internet Explorer 11, such as using an external library? <details> <summary>Here is the summary.</summary> <p>Lorem ipsum dolor sit amet</p> </details> App ...

I have a query regarding the implementation of trackballcontrols.js in three.js

I'm trying to incorporate trackballcontrols.js into my project but I need to make some modifications. I'm having trouble understanding this code snippet, could you provide some explanation? var calculateMousePosition = ( function () { var v ...

Using VueMultiselect with Vue 3: A guide for beginners

I'm currently experimenting with the vue multiselect component, but when I include it in the template, I am encountering a series of warnings and errors. <script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email ...

Problem with ngStyle: Error indicating that the expected '}' is missing

I encountered an error in the Chrome console when trying to interpret the code below: <div [ngStyle]="{'width': '100%'; 'height': '100%'; 'background-size': 'cover'; 'background-repeat&ap ...

What are the steps to ensure that data retrieved from an API is accurately displayed in a table?

My current goal is to collect data from the crypto compare API and display it in a table format. Although I am able to generate the necessary elements and append them to the table body, I am facing an unusual issue. Each time I use a for loop to iterate th ...

CASL user update has been refreshed

My CASL implementation is quite basic and I've noticed that the documentation lacks detail. The code I'm using is essentially a copy-paste from the docs: import { abilitiesPlugin } from '@casl/vue' import defineAbilitiesFor from &apos ...

Leveraging directives within AngularJS

I'm currently delving into the world of AngularJS and facing a particular question that has me stumped. Let's examine the sample code snippet I put together: js var app = angular.module('customerPortalApp', ["ui.router"]); app.confi ...

"Can someone guide me on the process of transmitting data to a client using Node in combination with

I am new to web development and struggling to understand how to transfer data from the Node server to the client while also displaying an HTML page. I am aware that res.send() is used to send data, but I'm having difficulty maintaining the client disp ...

Is there a way to eliminate span tags from elements?

I need to replace all the span tags with the class "article" and add new span tags to the content <div class="asd"> <span class="location">Foo</span> <span class="article">bar</span> <span ...