Next.js & Firebase Authentication Guide

by Allan Hillman, Front-End Engineer

Prerequisites and Project Setup


Before diving in, ensure Node.js is installed on your local machine. This can be downloaded from the Node.js official website. Once installed, we'll begin by setting up a new Next.js app using the npx create-next-app@latest command in your terminal. Follow the setup prompts, opting for the App recommended option and not the src folder option.

Next, install Tailwind CSS, ESLint, and then run npm update to keep everything up-to-date. Once all installations are complete, kickstart your development server with npm run dev, ensuring to use nvm use v18 for the correct Node version.


Note: Always remember to shut down the server when installing packages to avoid any errors.


Project Configuration: Cleaning and Coding


Our project will need some tweaking to fit our needs. This involves removing certain files and code sections, followed by adding some. For instance, you will need to clear out the main tag element in the page.js file and add a div and a h1 with a title of your choice.

In addition, you will need to add certain codes, and add all the btn classes to the global.css file. Always remember to update the metadata in layout.js with your own information.


Setting up Firebase in Next.js


Here's where it gets even more exciting! Firebase, a Backend-as-a-Service, offers services like authentication, database, storage, and hosting. For our project, we'll focus on authentication and database.


After creating a new project on Firebase Console, we'll enable Firebase Hosting and install the Firebase SDK in our CLI using npm install firebase. The process involves setting up Firebase in our Next.js app using a firebaseConfig object, and deploying the app to Firebase Hosting.


Setting up Firebase Authentication & Creating Protected Pages


Firebase Authentication allows users to sign up using their email and password. The onAuthStateChanged() function allows us to create protected pages that are only accessible to logged-in users. By utilizing the React Context API, we can share state with other components, allowing us to create an efficient authentication process.


Optional: Success & Error Handling with React Hot Toast


To wrap it up, we'll install React Hot Toast which allows us to display notifications seamlessly in our React app. This tool is instrumental in handling success and error messages and helps improve the overall user experience.


By following these steps, you're well on your way to setting up a full-stack app using Next.js and Firebase. This tutorial should equip you with the fundamental skills needed to create a robust and efficient web application. Happy coding!