Firebase Cookbook Over 70 recipes to help you create real-time web and mobile applications with Firebase Houssem Yahiaoui BIRMINGHAM - MUMBAI Firebase Cookbook Copyright © 2017 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: November 2017 Production reference: 1281117 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78829-633-5 www.packtpub.com Credits Author Copy Editor Houssem Yahiaoui Safis Editing Reviewer Project Coordinator Thomas David Kehoe Devanshi Doshi Commissioning Editor Proofreader Kunal Chaudhari Safis Editing Acquisition Editor Indexer Larissa Pinto Francy Puthiry Content Development Editor Graphics Aditi Gour Jason Monteiro Technical Editor Production Coordinator Sushmeeta Jena Deepika Naik About the Author Houssem Yahiaoui, a backend engineer at Xapo from Algeria, has almost 4 years, experience in building everything from backend services to mobile apps using Hybrid, Not so Hybrid and Native Approaches. Houssem is also a Telerik Developer Expert title holder and community power believer. He's also a Google Developer Group Lead Organizer, Algeria Tech Community (Community Manager), and speaker at numerous national and international events from DevFests to DroidCon, sharing his experience of web technologies, and a love for serverless approaches and Firebase. About the Reviewer Thomas David Kehoe makes technology for speech clinics to treat stuttering and other speech disorders. He is interested in how auditory processing affects second language acquisition. He develops web apps using Firebase, Angular, and JavaScript. www.PacktPub.com For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. https://www.packtpub.com/mapt Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career. Why subscribe? Fully searchable across every book published by Packt Copy and paste, print, and bookmark content On demand and accessible via a web browser Customer Feedback Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at www.amazon.in/dp/1788296338. If you'd like to join our team of regular reviewers, you can email us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products! Table of Contents Preface 1 Chapter 1: Firebase - Getting Started 7 Introduction 7 Creating your first Firebase application 8 How to do it... 8 Adding Firebase to an existing frontend project 11 How to do it... 11 How it works... 13 Integrating Firebase into the backend 14 How to do it... 14 How it works... 15 Integrating Firebase in Android applications 15 Getting ready 15 How to do it... 16 Integrating Firebase in iOS applications 24 Getting ready 24 How to do it... 24 Chapter 2: Firebase Real-Time Database 30 Introduction 30 Saving and serving data in and from our Realtime Database 31 How to do it... 31 How it works... 36 Updating and deleting data from our Realtime Database 36 How to do it... 36 How it works... 37 Structuring data within our Realtime Database 38 How to do it... 38 Implementing offline capabilities support 40 Getting ready 40 How to do it... 40 How it works... 40 Chapter 3: File Management with Firebase Storage 41 Introduction 41 Table of Contents Creating file storage references 42 Getting ready 42 How to do it... 42 How it works... 43 Implementing file upload 43 How to do it... 44 How it works... 49 Implementing file serving and downloading 50 How to do it... 50 How it works... 51 Implementing file deletion 51 How to do it... 52 Implementing file metadata updates 52 How to do it... 53 Firebase file storage error handling 53 How to do it... 54 Chapter 4: Firebase Authentication 56 Introduction 56 Implementing email/password authentication 57 How to do it... 58 Implementing anonymous authentication 61 How to do it... 61 Implementing Facebook login 62 Getting ready 63 How to do it... 67 Implementing Twitter login 70 Getting ready 71 How to do it... 72 Implementing Google Sign-in 74 Getting ready 75 How to do it... 75 How it works... 77 Implementing user metadata retrieval 77 How to do it... 78 How it works... 78 Implementing the linking of multiple authentication providers 78 How to do it... 79 How it works... 79 [ ii ]