This project serves as a guide to get started with Next.js and Firebase, demonstrating how to set up authentication with email/password and error handling with React Hot Toast.
Ensure you have Node.js installed on your local machine. If not, download it from Node.js official website.
- Install
Next.js
and create a new app inside current folder with out giving a project name:
-
`npx create-next-app@latest .`
will create a Next.js app and will require project name:
-
`npx create-next-app@latest`
will create a new Next.js app inside a folder named
my-app
. It will prompt you to enter the name of the project, this can be anything you want.
-
Install
Tailwind CSS
when prompted in the CLI -
Install
ESLint
-
Choose
App
recommend option, notsrc
folder option -
After installation is complete, run
npm update
-
Run
npm run dev
to start the development server. Make sure you usenvm use v18
to use the correct node version.
IMPORTANT NOTE
When installing packages, make sure to shut down the server and restart it again. This will ensure that the packages are installed correctly and you don't run into any errors.
REMOVE the following files from the project:
- Open the
app
folder, go topage.js
inside thereturn
statement, collapse themain
tag element and delete all of it. Add adiv
and ah1
, give it any title you want - Open the
public
folder delete thenext.svg
&vercel.svg
images - In the tailwind.config.js file, delete the
backgroundImage
object - In the
app
folder, delete theglobals.css
styles but keep the 3@tailwind
imports
ADD the following files to the project:
- In the
global.css
addhtml, body, :root {height: 100%;}
& ALL thebtn
classes - Open the
layout.js
file and update the metadata with your own information. You can also import any google fonts you like. For this project I usedMontserrat
- Open your CLI and run
npm i react-hot-toast
ornpm install react-hot-toast
oryarn add react-hot-toast
to install React Hot Toast - Import the
Toaster
component fromreact-hot-toast
, within theapp
folder, open thelayout.js
file, at the top of the fileimport { Toaster } from 'react-hot-toast'
When you are using Link
in Next.js
13 you need to import it from next/link
, be sure to NOT include in a
tag just pass the href
prop to the Link
component.
This is a custom component that will be used for the sign up and sign in pages. It will be located in the components
folder. Add this component to your files to handle the form layout.
Click to expand
Firebase is a BaaS - Backend-as-a-Service. It provides a lot of services like authentication, database, storage, hosting, etc. We will be using Firebase for authentication and database.
- Go to Firebase Console and click on ADD PROJECT to create a new project. You can name it anything you want.
- Enable analytics if you want to track your app usage (optional)
- Click on Continue
- On the homepage click on the Web Icon to create a new web app
- Give it a name and click on Register app
- Click on Box for Firebase Hosting and click on Next
- In your CLI run
npm i firebase
ornpm install firebase
oryarn add firebase
to install Firebase SDK -
Copy the firebaseConfig object
and paste it in a safe place, You will NEED this later! - Install the
Firebase CLI
run the command in CLInpm install -g firebase-tools
- Deploy your app to Firebase Hosting, run the command in CLI
firebase login
and login to your account - Run
firebase experiments:enable webframeworks
to enable the Next.js support - Run the command in CLI
firebase init
and select the following options:
Hosting: Configure and deploy Firebase Hosting sites
Use an existing project
Select the project you created
Answer Following Questions in CLI
- On the Project Overview page, You will use
Authentication
services, so click on Build then Authentication finally Get Started. This is where the users that sign up for our app will be stored - In the Authentication settings,
Choose Sign-in-method - add Email/Password
Remeber the firebaseConfig object you copied earlier? We will be using it now to set up Firebase in our Next.js app.
- Create a
.env
file at the root of the project and add the following environment variables:
NEXT_PUBLIC_FIREBASE_API_KEY="api-key"
NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN="auth-domain"
NEXT_PUBLIC_FIREBASE_PROJECT_ID="project-id"
NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET="storage-bucket"
NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID="sender-id"
NEXT_PUBLIC_FIREBASE_APP_ID="app-id"
NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID="analytic-id"
- Add the
.env
to.gitignore
, if you pushed up your code to GitHub, you don't want to expose yourAPI keys
. If you did run the commandgit rm -r --cached .env
to remove all the .env files from cache - Close down server and run
npm run dev
again to restart the server - Create a folder at the root level, name it
firebase
and create aconfig.js
file inside it
CONGRATS You have successfully set up Firebase in your Next.js app!
- Create a
auth
folder inside thefirebase
folder, then create asignup.jsx
andsignin.jsx
file
We want to know when users are logged in or logged out. We can create protected pages and only display certain contents to the logged in user. Firebase provides a method to listen for auth state changes onAuthStateChanged()
. Making the user data available globally is a good idea, we can do this utilizing the React Context API
allowing us to share state with other components.
- In the
root
directory, create a foldercontext
, then create aAuthContext.jsx
file inside it
To access the values passed to the AuthContext.Provider
, we are exporting useAuthContext
, now we can use the user
value.
In order to use the useAuthContext
hook, we need to wrap the AuthContext.Provider
around all the components in the layout.js
file located in the app
directory, Wrap the { children }
prop with AuthContext.Provider
Now we can create protected routes and only display certain contents to the logged in user!
- Open the
app
folder and create a new folderadmin
and add apage.jsx
file inside it. This will be a protected page that only logged in users can access
In Next.js 13, you create pages within the app
directory. Each page has it's own folder with a page.js
or page.jsx
file.
For Route Groups and Private Folders
you can create a folder with ( )
and add a page.js
or page.jsx
file inside of it. This will allow for Group routes without affecting routing.
For this project I created a (site)
folder with a page.jsx
to show the Sign Up page first.
- Delete the current
page.js
file that is in theapp
directory - Inside the
app
directory, create a(site)
folder with apage.jsx
file inside it - Next, create a
signin
folder inside the(site)
folder and add apage.jsx
file. - Create a
components
folder in the root directory and add aFormControls.jsx
file inside it. This way we can reuse the form controls for both the sign up and sign in pages - Import the
FormControls
component into the Sign-Up page located in the(site)
folderpage.jsx
file & the Sign-In page located in the(site)/signin
folder within thepage.jsx
file
Congrats! You have successfully created a protected page & setup Firebase Authentication in your Next.js app! This is completing the basics of creating a simple login with just a email and password
Click to expand
Add seamless notifications to your React app with ease.
- Run one of these commands in your CLI
npm i react-hot-toast
ornpm install react-hot-toast
oryarn add react-hot-toast
- Import
Toaster
fromreact-hot-toast
in theapp
folder,layout.js
file, your import should look like thisimport { Toaster } from 'react-hot-toast'
- Add the
Toaster
component below theAuthContext.Provider
component in thelayout.js
file - In the
(site)
folderpage.jsx
file,import { toast } from 'react-hot-toast'
- Add the following code to the error and success handlers:
toast.error("Sign up failed!")
&toast.success("Sign up successful!")
- Next, add them to
signin
folder,page.jsx
file
If you see the messages appear in the browser, you have successfully set up React Hot Toast!
The project was inspired by the article on setting up Next.js & Firebase. Although several modifications have been made, it's important to give due credit.