Ben Gorman

Ben Gorman

Life's a garden. Dig it.

To understand Firebase auth, we need a toy project to tinker with. I'll keep it pretty bare-bones for now. I'll call this project fireauth.

Here's the gist...

  1. Initialize a Next.js project with create-next-app.
  2. Initialize Firebase services: Firestore, Functions, and Emulator using firebase init from the Firebase CLI.
  3. Initialize the firebase app inside a firebase.js file, as explained here.

Here's an overview of the project structure and files involved šŸ‘‡

fireauth
ā”œā”€ā”€ .env.production
ā”œā”€ā”€ .firebaserc
ā”œā”€ā”€ .git
ā”‚   ā””ā”€ā”€ ...
ā”œā”€ā”€ .gitignore
ā”œā”€ā”€ .vscode
ā”‚   ā”œā”€ā”€ settings.json
ā”‚   ā””ā”€ā”€ tasks.json
ā”œā”€ā”€ README.md
ā”œā”€ā”€ firebase.json
ā”œā”€ā”€ firestore-debug.log
ā”œā”€ā”€ firestore.indexes.json
ā”œā”€ā”€ firestore.rules
ā”œā”€ā”€ functions
ā”‚   ā”œā”€ā”€ .gitignore
ā”‚   ā”œā”€ā”€ index.js
ā”‚   ā”œā”€ā”€ package-lock.json
ā”‚   ā””ā”€ā”€ package.json
ā”œā”€ā”€ jsconfig.json
ā”œā”€ā”€ next.config.mjs
ā”œā”€ā”€ package-lock.json
ā”œā”€ā”€ package.json
ā”œā”€ā”€ src
ā”‚   ā”œā”€ā”€ app
ā”‚   ā”‚   ā”œā”€ā”€ layout.js
ā”‚   ā”‚   ā””ā”€ā”€ page.js
ā”‚   ā””ā”€ā”€ firebase
ā”‚       ā””ā”€ā”€ firebase.js
ā””ā”€ā”€ ui-debug.log
# Development settings and API keys go here
# This file should not be tracked by git
 
### Firebase keys
NEXT_PUBLIC_FIREBASE_API_KEY=AIzaSyRUHG9ZP-vUWBqkPU5I
NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=fireauth.firebaseapp.com
NEXT_PUBLIC_FIREBASE_PROJECT_ID=fireauth
NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET=fireauth.appspot.com
NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=156895892077
NEXT_PUBLIC_FIREBASE_APP_ID=1:1569792077:web:7263700fbcb18fa02564
export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>{children}</body>
    </html>
  )
}
export default function HomePage() {
  return <div>Home page</div>
}
// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app"
import { connectAuthEmulator, getAuth } from "firebase/auth"
import { connectFirestoreEmulator, getFirestore } from "firebase/firestore"
import { connectFunctionsEmulator, getFunctions } from "firebase/functions"
 
// 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)
 
// Instantiate services
export const auth = getAuth(app)
export const db = getFirestore(app)
export const functions = getFunctions(app)
 
// Emulator
if (process.env.NODE_ENV == "development") {
  connectAuthEmulator(auth, "http://127.0.0.1:9099")
  connectFunctionsEmulator(functions, "127.0.0.1", 5001)
  connectFirestoreEmulator(db, "127.0.0.1", 8080)
}

At this point, you should be able to start the Next.js application with the shell command

$ npm run dev
Run this from the working directory: fireauth/

And you should be able to start the Firebase Emulator services with the shell command

$ firebase emulators:start --debug

If you open your browser and go to http://localhost:3000/ you should see (very simple) home page.

Home page