
Cloud
Learning Level
// Multi-region configuration
const regions = {
us: {
database: 'my-app-us.firebaseio.com',
projectId: 'my-app-us',
region: 'us-central1'
},
eu: {
database: 'my-app-eu.firebaseio.com',
projectId: 'my-app-eu',
region: 'europe-west1'
},
asia: {
database: 'my-app-asia.firebaseio.com',
projectId: 'my-app-asia',
region: 'asia-east1'
}
};
// Route users to nearest region
function getUserRegion(userLocation) {
const [latitude, longitude] = userLocation;
return determineOptimalRegion(latitude, longitude);
}import admin from 'firebase-admin';
async function createCustomToken(userId, claims) {
const token = await admin.auth().createCustomToken(userId, {
role: claims.role,
team: claims.team,
issuedAt: admin.firestore.FieldValue.serverTimestamp()
});
return token;
}
// Verify token with additional validation
async function verifyTokenWithClaims(token) {
const decodedToken = await admin.auth().verifyIdToken(token);
const role = decodedToken.role || 'user';
return { uid: decodedToken.uid, role };
}import { getPerformance, trace } from "firebase/performance";
const perf = getPerformance();
async function optimizedQuery(query) {
const myTrace = trace(perf, 'query-operation');
myTrace.start();
const result = await query.get();
myTrace.putAttribute('doc_count', result.docs.length);
myTrace.putMetric('latency_ms', Date.now() - startTime);
myTrace.stop();
return result;
}exports.auditLog = functions.firestore
.document('sensitive/{docId}')
.onWrite(async (change, context) => {
const audit = {
timestamp: admin.firestore.FieldValue.serverTimestamp(),
userId: context.auth?.uid,
action: change.before.exists ? 'update' : 'create',
before: change.before.data(),
after: change.after.data()
};
await admin.firestore().collection('audit_logs').add(audit);
});const cache = new Map();
const CACHE_TTL = 5 * 60 * 1000;
async function getCachedData(key, fetchFn) {
const cached = cache.get(key);
if (cached && Date.now() - cached.timestamp < CACHE_TTL) {
return cached.data;
}
const data = await fetchFn();
cache.set(key, { data, timestamp: Date.now() });
return data;
}Implement microservices patterns with Cloud Functions, or design real-time collaborative systems.
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep โข Build Real โข Verify Skills โข Launch Forward