Authenticating Users with Google Sign In and Firebase\Firestore inside of Dart's Flutter Framework

in #utopian-io6 years ago (edited)

flutter-logo.jpg

Repository

https://github.com/flutter/flutter

What Will I Learn?

  • You will learn how to use add authentication to Firebase/Firestore projects
  • You will learn how to use OAuth 2.0 with Firebase/Firestore
  • You will learn how to use image filter widgets in flutter
  • You will learn how to manage singleton object in a provider class
  • You will learn how to use assertions and logic to verify data

Requirements

System Requirements:
OS Support for Flutter:
  • Windows 7 SP1 or later (64-bit)
  • macOS (64-bit)
  • Linux (64-bit)

Required Knowledge

  • A little understanding of key/value stores
  • A fair understanding of Mobile development and Imperative or Object Oriented Programming
  • Some understanding of realtime databases

Resources for Flutter and this Project:

Sources:

Flutter Logo (Google): https://flutter.io/

Difficulty

  • Intermediate

Description

Outline and Overview

In this Flutter Video Tutorial, we take a look at how it is possible to authenticate a user using the Google Sign In plugin inside of Firebase and Firestore. The application uses a login page which allows the user to navigate to the home page only if the user is authenticated by Google. Google sends a token to the Firebase console which them verifies that the user is authenticated. If they are authenticated then the user can read and write to our Firestore database. We also make use of the Image Filter Widget in this application to allow us to create a unique aesthetic for our login page. The Singleton pattern is also used and we use assertions in our provider logic.

Outline for this Tutorial
Item 1: Authentication via Google for Firebase/Firestore

There are various reasons why you would want to authenticate a user in a mobile application. With Firebase we are able to not only restrict which users can read and write from our database, we can also use the authentication to identify the users. This allows us to preform analytics as well as save security data in the cloud. We are also able to personalize the application experience with this information to increase application retention.

fb_rules.jpg

This image shows the rules that were used for our cloud firestore database in this application. These rules only allow a user to read and write our data if the request they make is authenticated beforehand. This rule set is very simple but it can be expanded in different ways for various types of applications.

Item 2: Using OAuth 2.0 Inside of Flutter

One of the larger advantages of using the Firebase Authentication system is that it makes use of the OAuth 2.0 standard. This allows us to authenticate our users from a single click. We also avoid potential security risks which come from using a more traditional validation system that actually requires that the user inputs their email and password into our application.

fb_static.png

This image shows the method that we use to authenticate the user inside of our application. First, we ask Google to authenticate the user by having them sign in to the Google Playstore or another Google application. We then are able to get the Authentication Tokens from this sign in event through our GoogleSignInAuthentication object. These tokens are passed into the FirebaseUser object which then confirms that the user is indeed who they say they are. The information is then passed back into firebase on a successful authentication.

Item 3: Using the Singleton Pattern to Fetch Consistent Data

Firebase Authentication and the libraries surrounding Firebase all use the Singleton Pattern. This is useful because it allows the application to keep the same instance of the object consistent across the entire code base. In this case, we are able to easily pass the user data from the API provider to the view layer of our application.

streambuilder-2.png

Just as we were able to use a Stream Builder widget to dynamically change the widget tree in the base application, we are also able to display information about the user to our home screen. In this case, we are taking the FirebaseAuth instance object and calling the currentUser method on it to get the currently signed in user. We are then able to access any piece of information associated with this user in the widget tree below this Stream Builder.

The source code for this project can be found here

The Commands and Firebase Rules used in this tutorial can be found here

Video Tutorial

Related Videos

Projects and Series

Stand Alone Projects:
Building a Calculator
Movie Searcher Application

Minesweeper Game

Weather Application

Curriculum

Proof of Work Done

https://github.com/tensor-programming

Sort:  

Your contribution has been evaluated according to Utopian rules and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post,Click here

Great work @tensor.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

As always, thank you for moderating my contribution.

Hey @tensor
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

You just received a Tier 0 upvote! Looking for bigger rewards? Click here and learn how to get them or visit us on Discord

Here we go again...

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.034
BTC 64455.55
ETH 3147.84
USDT 1.00
SBD 3.94