March 9, 2026 • Week 10
StyledFirebaseAuth component from react-firebaseuinpm install https://github.com/joelwross/firebaseui-web-reactcredentialHelper, and callbacksimport { 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()} />
onAuthStateChanged() listens for login/logout eventsnull if signed out)uid, displayName, email, photoURLuseEffect with a cleanup function to unregister the listenerimport { 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
}, []);
signOut(auth) to log the user out.catch() for error handlingonAuthStateChanged listener will fire with null when the user signs outimport { getAuth, signOut } from 'firebase/auth';
const auth = getAuth();
signOut(auth)
.catch(err => console.log(err));
auth !== null in rules to require authentication for read/writeauth.uid to restrict access to the user's own data{
"rules": {
"users": {
"$uid": {
".read": "auth !== null && auth.uid === $uid",
".write": "auth !== null && auth.uid === $uid"
}
},
"public": {
".read": true,
".write": "auth !== null"
}
}
}
storageRef(storage, "path/to/file")uploadBytes(), then get the URL with getDownloadURL()async/awaitimport { 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
}
uid// 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);
<input type="file"> element to let users select filesevent.target.files[0]const handleChange = (event) => {
if (event.target.files.length > 0) {
const chosenFile = event.target.files[0];
// upload chosenFile to Firebase Storage
}
};
StyledFirebaseAuth)uid, displayName, email, photoURLauth !== null requires login, auth.uid restricts to own datauploadBytes() and getDownloadURL()