Firebase, A Thank You Letter

When we set out to build our community-driven history app, we knew a strong foundation was essential for two key aspects: user authentication and cloud storage. After exploring various options, we decided on Firebase—and it proved to be one of the best choices we made for the project.

Why Firebase?

Our requirements were clear:

Firebase stood out due to its seamless integration with Flutter, comprehensive documentation, and generous free tier. Though we were initially cautious about depending on a third-party service, Firebase quickly proved its value.

Authentication: Simple and Effective

Setting up Firebase Authentication was straightforward. With just a few lines of code, we implemented email/password login and Google Sign-In, making the authentication process both simple and efficient:

				
					// Google Sign-In with Firebase  
Future<UserCredential> signInWithGoogle() async {  
  final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();  
  final GoogleSignInAuthentication googleAuth = await googleUser!.authentication;  
  final OAuthCredential credential = GoogleAuthProvider.credential(  
    accessToken: googleAuth.accessToken,  
    idToken: googleAuth.idToken,  
  );  
  return await FirebaseAuth.instance.signInWithCredential(credential);  
}  
				
			

The process was seamless, with Firebase handling everything from session management to token refreshes. Having such a critical part of the app work reliably with minimal effort was a huge relief.

Cloud Storage: Reliable and Scalable

Storing user-uploaded media was another challenge, but Firebase Cloud Storage made it effortless:

				
					// Uploading a file to Firebase Storage  
Future<String> uploadFile(File file, String userId) async {  
  final storageRef = FirebaseStorage.instance.ref().child('user_uploads/$userId/${file.path}');  
  await storageRef.putFile(file);  
  return await storageRef.getDownloadURL();  
}  
				
			

Whether it was a photo of a historic building or a video of a wooden escalator (yes, tourists really do visit Antwerp for that—lol), Firebase Cloud Storage handled it all seamlessly. The ability to generate download URLs on the fly made integration with our .NET backend effortless.

The Challenge: Securing the API Key

One of the first hurdles we faced was securing the Firebase API key. Flutter doesn’t make it easy to hide sensitive keys, and hardcoding them in google-services.json or GoogleService-Info.plist poses a security risk.

After some research, we implemented a workaround using environment variables and Firebase Cloud Functions to handle sensitive operations securely:
				
					// Using environment variables to hide the API key  
const firebaseConfig = {  
  apiKey: process.env.FIREBASE_API_KEY,  
  authDomain: process.env.FIREBASE_AUTH_DOMAIN,  
  projectId: process.env.FIREBASE_PROJECT_ID,  
  storageBucket: process.env.FIREBASE_STORAGE_BUCKET,  
  messagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID,  
  appId: process.env.FIREBASE_APP_ID,  
};  
				
			

While not a perfect solution, it was a necessary step to enhance our app’s security and protect sensitive data.

Why Firebase Worked for Us

  1. Ease of Use: Firebase’s Flutter SDK is well-documented and easy to integrate.
  2. Scalability: Whether for a small prototype or a global app, Firebase scales with us.
  3. Google Integration: Google Sign-In and Firebase work seamlessly together.
  4. Real-Time Capabilities: While we didn’t use Firestore this time, it remains a valuable option for future features.

A Few Words of Advice

If you’re using Firebase with Flutter:

  • Secure Your API Keys: Use environment variables or backend services to keep sensitive data safe.
  • Leverage Firebase Functions: Offload complex logic to the cloud for better security and performance.
  • Test on Both Platforms: Firebase works well on Android and iOS, but always test thoroughly on both.

Conclusion

Firebase was a critical part of our app. It provided a reliable, scalable solution for authentication and cloud storage, allowing us to focus on building the app’s core features like Augemented Reality. While there were challenges, like securing the API key, Firebase’s benefits far outweighed the drawbacks.

To anyone building a Flutter app: Firebase is a powerful tool worth considering. It’s not without its quirks, but it gets the job done, and it does it well.

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents

SYNNOVATION