← Back to Week 10

Firebase Authentication

March 9, 2026 • Week 10

Topics Covered

Firebase Authentication Overview

FirebaseUI

import { getAuth, GoogleAuthProvider, EmailAuthProvider } from 'firebase/auth';
import StyledFirebaseAuth from 'react-firebaseui/StyledFirebaseAuth';

const firebaseUIConfig = {
    signInOptions: [
        GoogleAuthProvider.PROVIDER_ID,
        { provider: EmailAuthProvider.PROVIDER_ID, requireDisplayName: true }
    ],
    signInFlow: 'popup',
    credentialHelper: 'none',
    callbacks: {
        signInSuccessWithAuthResult: () => false
    }
};

// Render the sign-in UI
<StyledFirebaseAuth uiConfig={firebaseUIConfig} firebaseAuth={getAuth()} />

Managing Auth State

import { getAuth, onAuthStateChanged } from 'firebase/auth';

useEffect(() => {
    const auth = getAuth();
    const unregister = onAuthStateChanged(auth, (firebaseUser) => {
        if (firebaseUser) {
            // user is signed in
            console.log(firebaseUser.uid);
            console.log(firebaseUser.displayName);
        } else {
            // user is signed out
        }
    });

    return () => unregister(); // cleanup on unmount
}, []);

Signing Out

import { getAuth, signOut } from 'firebase/auth';

const auth = getAuth();
signOut(auth)
    .catch(err => console.log(err));

Security Rules with Auth

{
    "rules": {
        "users": {
            "$uid": {
                ".read": "auth !== null && auth.uid === $uid",
                ".write": "auth !== null && auth.uid === $uid"
            }
        },
        "public": {
            ".read": true,
            ".write": "auth !== null"
        }
    }
}

Firebase Storage

import { getStorage, ref as storageRef, uploadBytes, getDownloadURL } from 'firebase/storage';

const storage = getStorage();
const fileRef = storageRef(storage, "images/profile.png");

async function uploadFile(file) {
    await uploadBytes(fileRef, file);
    const url = await getDownloadURL(fileRef);
    // save url to Realtime Database
}

Storing Additional User Data

// Database structure for extra user data
{
    "userData": {
        "userId1": { "pronouns": "they/them", "bio": "Hello!" },
        "userId2": { "pronouns": "he/him", "bio": "World!" }
    }
}

// Access with:
const userRef = ref(db, "userData/" + currentUser.uid);

File Input Handling

const handleChange = (event) => {
    if (event.target.files.length > 0) {
        const chosenFile = event.target.files[0];
        // upload chosenFile to Firebase Storage
    }
};

Key Concepts

Resources