Enhance your web app with NextJS and Firebase authentication

Currently, I am in the process of setting up a small Next.js application to work with a Firebase database (including storage).

One issue I encountered is that the Firebase app instance is located in the server components because I was concerned about sending sensitive 'credentials' (such as the API key) to the client.

However, I discovered that managing the app instance's lifetime in the server components was problematic, as it seemed to be inconsistent or difficult to control (despite having a singleton there). This led me to recreate the instance on every request.

Realizing the shortcomings of my initial setup, I made the decision to move the Firebase instance to the client components instead.

Yet, this approach raises the concern of exposing the API key, app id, and other sensitive information to the client. I am now seeking guidance on the proper and secure way to handle this situation.

(I have come across resources suggesting the use of 'env variables' prefixed with NEXT_PUBLIC to pass these details to the client, which is the method I ultimately adopted.)

Answer №1

If you are utilizing the Firebase web SDK, there is no need to be concerned about the keys used to initialize the app. These keys are simply meant for Firebase to identify your app and are not utilized to control access to database or Cloud Storage data. For more information, refer to https://firebase.google.com/support/guides/security-checklist#api-keys-not-secret

However, when using the Firebase admin SDK, you have the option to create internal API routes to retrieve necessary data from Firestore or cloud database. Subsequently, you can utilize these API routes within your client components without the necessity of public keys in your front-end.

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

AngularJS url update event

In order to gather statistical data, I am interested in tracking how many times the URL has been changed. To achieve this, I have set up a counter that will increment each time the URL is modified. Does anyone have a solution for this? ...

Adding information into MongoDb with the help of mongoose

I am currently working with an array of strings and my goal is to iterate through this array and update my collection with its values. This is the approach I have taken: if (employees) { employees.map((employee) => { Employee.updateOne({ ...

How to Implement Filtering in AngularJS ng-repeat

While developing my app, I came across a challenge with the filter in my ng-repeat. Initially, I could search by productCode or name. Then, a new requirement emerged - "Filter by SubcategoryId." The issue lies in filtering strictly by subCategoryId, while ...

Is there a way to transfer the functionality of openssl_seal from PHP to NodeJS 18?

I'm looking to convert the openssl_seal() function from PHP to NodeJs. The code below is from my PHP SDK and works flawlessly, which is what I want to migrate: $ivLength = openssl_cipher_iv_length('AES-256-CBC') ?: 16; $iv = openssl_random ...

Guide to setting up the airtable.js module on your system

Perhaps this question might seem silly, but I am brand new to JavaScript and I am trying to use the Airtable API. I downloaded Airtable.js from https://github.com/Airtable/airtable.js, extracted the files to my D: drive, and then attempted to type "npm ins ...

How can I create a form in Django where selecting a drop-down option triggers an automatic submission?

I'm attempting to create a form that can pre-fill fields based on user selection from a drop-down menu. To kick things off, I want the form to automatically submit when an option is chosen from the drop-down list. Here is the code snippet I have been ...

Eliminate the Div Attribute once the radio button is activated

I'm just starting out with JavaScript and feeling a bit lost. I came across some code that adjusts the attributes of a Div based on a selection from a dropdown list. Now, I want to tweak it so that it works when a radio button is selected instead. Be ...

What is the best way to reload scripts each time a component is mounted?

My jQuery scripts include animation effects that need to be refreshed whenever something new is rendered on the page. However, I am facing an issue where the jQuery scripts are not being refreshed as needed. Below is my router configuration: export defau ...

Can you guide me on implementing CSS Houdini in Next.js?

Exploring the world of CSS Houdini in my Next.js project has been quite an adventure. After successfully installing the necessary CSS Houdini package and css-paint-polyfill using yarn, I decided to follow the webpack guidelines provided at . Below is a sn ...

Set a NodeJS variable equal to a specific value based on an array within a MongoDB document

Within my MongoDB collection, I currently have the following documents: > db.mycol.find() { "_id" : ObjectId("5ec6506171ae442136aa97d2"), "uname" : "mail1", "port" : 1000, "abc" : "test1" } { "_id" : ObjectId("5ec659e6c0b1cc11370d8378"), "uname" : "mai ...

NextJS authentication: choose between Passport.js or Next-Auth

Currently, my project is in progress with Next.js. I have previously worked on a MEAN app where I utilized passport.js for authentication. In Next.js, I've learned that I have the option of using either passport.js or next-auth for authentication pur ...

What is the best way to connect individual buttons to a dynamic div that displays different content depending on the button clicked?

Hey there! I'm diving into the world of JavaScript and HTML, and I need some guidance on creating a menu that can toggle visibility of specific content in div(s) depending on which button (picture1-12) is clicked. My idea is to have one div that can d ...

Dispatching actions in `componentDidMount` is restricted in Redux

Update at the bottom of post I've created a React container component called AppContainer, which checks if the user is authenticated. If the user is authenticated, it renders the app's routes, header, and content. If not, it displays a Login com ...

Using JQuery to load a table and inject it with html()

I am looking to populate an HTML table within a specific div element. The HTML code is being loaded using the following jQuery function: $("#table_wrapper").hide(); $.get("<?echo base_url();?>schichtplan/employee_fields/"+plan_id+"true",function(da ...

Comparing nestableSortable with the Nestable JavaScript library

I am in the process of developing a navigation menu editor that consists of multiple nested, sortable forms. My goal is to submit all the form data in one comprehensive JSON data blob. After researching, I have narrowed down my options to two libraries: n ...

Dropdown menu utilizing processing API and interacting with AJAX and DOM manipulation

My API data is not showing up in the dropdown menu. If I use ?act=showprovince, I can see the result. example.html <head> <link rel="stylesheet" type="text/css" href="css/normalize.css"> <link rel="stylesheet" type="text/css" hr ...

Navigate forward to the next available input in a grid by using the tab key

My goal is to improve form entry efficiency by using the tab key to focus on the next empty input field within a grid component. If an input field already has a value, it will be skipped. For example, if the cursor is in input field 2 and field 3 is filled ...

javascriptcode to execute before loading google maps

I am facing an issue with displaying markers on Google Maps. The problem arises when the array with latitude and longitude values is constructed through an Ajax request. Due to the map being loaded before the initialization of the array, I am unable to see ...

The IF ELSE Statement will consistently evaluate as true when the first IF condition is met

I am currently working on implementing an IF ELSE statement in JavaScript to change the source image of a picture when a button is clicked. The idea is that when a user clicks on a specific body type button for a car, it will update the background image wi ...

The `react-hover` npm package functions flawlessly in the development environment despite being excluded from the production build

While working on my project, I decided to utilize the npm package react-hover and it's been effective during local development in dev build. However, when I execute the npm run build command to serve the production version, the components within the & ...