Discover a step-by-step guide to integrating Firebase auth in your Next.js app. From project setup to implementing a signup form, boost your app’s authentication today.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Set Up Your Firebase Project
Step 2: Create a Next.js Project
Open your terminal and run:
npx create-next-app@latest my-firebase-app
Step 3: Install Firebase SDK
Navigate into your project directory and install the Firebase SDK:
cd my-firebase-app
npm install firebase
Step 4: Configure Firebase in Next.js
firebaseConfig.js in the root of your Next.js project.firebaseConfig.js, add the following configuration:
import { initializeApp } from 'firebase/app';
import { getAuth } from 'firebase/auth';
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER\_ID,
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
// Initialize Firebase Authentication and get a reference to the service
export const auth = getAuth(app);
firebaseConfig with your actual Firebase project credentials from the Firebase Console..env.local file in the root of your project directory and add:
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
Step 5: Implement Authentication Logic
auth inside the pages directory.signup.js inside the auth directory.signup.js:
import { useState } from 'react';
import { auth } from '../firebaseConfig';
import { createUserWithEmailAndPassword } from 'firebase/auth';
const Signup = () => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [error, setError] = useState(null);
const handleSignup = async (e) => {
e.preventDefault();
try {
const userCredential = await createUserWithEmailAndPassword(auth, email, password);
console.log('User signed up:', userCredential.user);
} catch (error) {
setError(error.message);
}
};
return (
Sign Up
{error && {error}
}
);
};
export default Signup;
Step 6: Run and Test Your Application
In the terminal, run your application with:
npm run dev
http://localhost:3000/auth/signup.auth directory.
Conclusion
Congratulations! You have successfully integrated Firebase Authentication with a Next.js application. You can now expand on this setup to include additional authentication services, handle authentication state, and implement more complex application flows based on your project's requirements.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.