-
Notifications
You must be signed in to change notification settings - Fork 19
3. Installation
This guide provides step-by-step instructions on how to set up and configure a Next.JS application with Firebase integration. Firebase is a platform developed by Google that offers various backend services, such as authentication, database, storage, and cloud functions. By following these instructions, you will learn how to obtain Firebase secrets, enable authentication methods, set up Firestore database and indexing, configure storage (if needed), set up cloud functions, and run the Next.JS project.
These are the components needed to run the project.
- Node: 18 (LTS)
- Firebase: v9
To clone the project repository to your local machine, open a terminal and run:
git clone https://github.com/mbeps/next_discussion_platform.git
- Navigate to the project directory:
cd next_discussion_platform
- Copy the
.env.example
file and rename it to.env.local
:
cp .env.example .env.local
- Open the
.env.local
file and populate it with the required Firebase secrets.
These are step-by-step instructions on how to configure Firebase for the Next.JS application. It covers obtaining Firebase secrets, enabling authentication methods, setting up Firestore database and indexing, configuring storage, and enabling cloud functions.
- Go to the Firebase Console.
- Click on "Add project" to create a new Firebase project.
- Fill in the required details, enable Google Analytics if desired, and create the project.
- In your Firebase project, go to "Project settings" by clicking on the gear icon in the top left corner.
- In the "General" tab, scroll down to the "Your apps" section and click on the "</>" icon to add a web app.
- Fill in the app details and register the app.
- After registering, you will be provided with a Firebase SDK configuration object. Copy the values of
apiKey
,authDomain
,projectId
,storageBucket
,messagingSenderId
, andappId
. - Open the
.env.local
file in your Next.JS project and populate it with the copied Firebase secrets:
NEXT_PUBLIC_FIREBASE_API_KEY<your-api-key>
NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=<your-auth-domain>
NEXT_PUBLIC_FIREBASE_PROJECT_ID=<your-project-id>
NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET=<your-storage-bucket>
NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=<your-messaging-sender-id>
NEXT_PUBLIC_FIREBASE_APP_ID=<your-app-id>
These are required to allow you to authenticate into the app using these providers:
- In your Firebase project, go to the "Authentication" section from the left-side menu.
- Click on the "Sign-in method" tab.
- Enable and configure the desired sign-in providers, such as GitHub, Google, and Email/Password.
For GitHub authentication:
To allow you to use your GitHub account to sign into Circus, go to your GitHub account:
- Go to GitHub Developer settings.
- Click on "New OAuth App" and fill in the required details. Use the "Authorized callback URL" provided by Firebase.
- After registering, copy the "Client ID" and "Client Secret".
- Paste them into the GitHub authentication configuration in Firebase.
For Google authentication:
To allow you to use your Google account to sign into Circus, go to your Google account:
- Click on "Google" in the sign-in providers list.
- Enable the provider and fill in the required details, such as the "Project support email".
- Save the changes.
Firebase indexing is used to improve query performance by requiring an index for every query. The indexes required for the most basic queries are automatically created for you. As you use and test your app, Cloud Firestore generates error messages that help you create additional indexes your app requires.
- In your Firebase project, go to the "Firestore Database" section from the left-side menu.
- Click on "Create database" and choose the "Start in production mode" or "Start in test mode" based on your preference.
- Select a Cloud Firestore location and enable the database.
- In the "Indexes" tab, click on "Add index" and set up the following indexes:
Collection ID | Fields Indexed |
---|---|
posts |
communityId Ascending createdAt Descending name Descending |
comments |
postId Ascending createdAt Descending name Descending |
This is required to allow Firebase to store post images.
- In your Firebase project, go to the "Storage" section from the left-side menu.
- Click on "Get started" and follow the prompts to set up a Cloud Storage bucket.
These cloud functions are used in this project to automatically add and remove users to and from the Firestore database as users are created and deleted to and from Firebase Authentication.
- Ensure you have installed Firebase tools globally on your system. If not, run:
npm install -g firebase-tools
- Log in to Firebase:
firebase login
- In your Next.JS project directory, initialize the Firebase project:
firebase init
When prompted, follow these selections:
- Select
Functions: Configure a Cloud Functions directory and its files
by pressing Space to select and Enter to submit. - Choose
Use an existing project
. - Select the appropriate project for
Select a default Firebase project for this directory
. - Choose
TypeScript
forWhat language would you like to use to write Cloud Functions?
. - Select
Y
(yes) forDo you want to use ESLint to catch probable bugs and enforce style?
. - Choose
Y
(yes) forDo you want to install dependencies with npm now?
.
- Navigate to the
functions
directory:
cd functions
-
Develop your cloud functions in the
src
directory using TypeScript. Use theindex.ts
file as the main entry point for your functions. -
Compile your TypeScript code by running:
npm run build
- Deploy the cloud functions to Firebase:
firebase deploy --only functions
- In your terminal, navigate back to the root of the Next.JS project directory:
cd ..
- Install the required dependencies:
npm install
- Run the development server:
npm run dev
The project should now be running on http://localhost:3000
.