<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Raajeev Chandran</title>
    <description>The latest articles on DEV Community by Raajeev Chandran (@raajeevchandran).</description>
    <link>https://dev.to/raajeevchandran</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F519921%2F4d083a14-c703-406b-a38e-df23f068e214.png</url>
      <title>DEV Community: Raajeev Chandran</title>
      <link>https://dev.to/raajeevchandran</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/raajeevchandran"/>
    <language>en</language>
    <item>
      <title>Firebase Authentication and keeping users logged in with Provider in Flutter.</title>
      <dc:creator>Raajeev Chandran</dc:creator>
      <pubDate>Tue, 24 Nov 2020 05:37:28 +0000</pubDate>
      <link>https://dev.to/raajeevchandran/firebase-authentication-and-keeping-users-logged-in-with-provider-in-flutter-9j5</link>
      <guid>https://dev.to/raajeevchandran/firebase-authentication-and-keeping-users-logged-in-with-provider-in-flutter-9j5</guid>
      <description>&lt;p&gt;Incorporating firebase authentication into a flutter application can now be done in no-time, but the conventional way of doing so doesn’t turn out to be user-friendly in any manner. We just can’t expect our users to be keyboard-warriors and make them tolerant of signing in every-time they get their hands on our application.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;So, let’s fix this user experience clutter by keeping the user logged in by managing the state with the Provider package until the user smashes that “Sign Out” button.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We are just gonna be managing the state of our application with the Provider and orchestrate the execution flow based on whether the user is “Signed in” or not.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F875%2F1%2Azb3LeP930ioiWfOG3XZEdA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F875%2F1%2Azb3LeP930ioiWfOG3XZEdA.jpeg" alt="Architecture"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What is Provider and why is it worthwhile to use?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;On the bottom line, we are about to watch over a value(in this case, logged in or not) and take requisite actions. So typically we are gonna be listening to a value and watch over its changes to manipulate the workflow of our application. And this is where the Provider comes into play, by the time you read this article, some might have figured out what’s really powering this Provider under the hood. The Provider is just an incarnation of the &lt;a href="https://api.flutter.dev/flutter/widgets/InheritedWidget-class.html" rel="noopener noreferrer"&gt;InheritedWidget&lt;/a&gt; and &lt;a href="https://api.flutter.dev/flutter/foundation/ChangeNotifier-class.html" rel="noopener noreferrer"&gt;ChangeNotifier&lt;/a&gt; which lets you expose/create/listen/dispose a value. So deciphers the &lt;a href="https://pub.dev/packages/provider" rel="noopener noreferrer"&gt;offical documentation&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s now deep dive into the to-dos.&lt;/p&gt;

&lt;p&gt;Open the pubspec.yaml file in your Flutter project and add the following packages. (&lt;a href="https://pub.dev/packages/provider" rel="noopener noreferrer"&gt;provider&lt;/a&gt; ,&lt;a href="https://pub.dev/packages/firebase_auth" rel="noopener noreferrer"&gt;firebase_auth&lt;/a&gt; , &lt;a href="https://pub.dev/packages/firebase_core" rel="noopener noreferrer"&gt;firebase_core&lt;/a&gt;)&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We’ll kick-start this application by first finishing up with our Provider. Create an &lt;strong&gt;authentication.dart&lt;/strong&gt; file in the lib directory.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Import the dependencies.&lt;/li&gt;
&lt;li&gt;Create an &lt;strong&gt;AuthenticationProvider&lt;/strong&gt; class with a firebaseAuth instance.
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;import 'package:firebase_auth/firebase_auth.dart';&lt;br&gt;
class AuthenticationProvider{&lt;br&gt;
final FirebaseAuth firebaseAuth;&lt;br&gt;
// FirebaseAuth instance &lt;br&gt;
AuthenticationProvider(this.firebaseAuth);&lt;br&gt;
//Constructor to initialize the Firebase Auth instance.&lt;br&gt;
}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;We’ll use a Stream to continuously listen to the Authentication State(logged in or not).&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Stream&amp;lt;User&amp;gt; get authStateChanges =&amp;gt; firebaseAuth.idTokenChanges();&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Let’s quickly add the Sign-Up, Sign in, and Sign out methods.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We have now finished setting up our Authentication Provider. Let’s now fit this provider into our &lt;strong&gt;main.dart&lt;/strong&gt; file.&lt;br&gt;
1.In the &lt;strong&gt;main.dart&lt;/strong&gt; file, import the following dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import 'package:flutter/material.dart';&lt;br&gt;
import 'package:firebase_core/firebase_core.dart';&lt;br&gt;
import 'package:provider/provider.dart';&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;2.We are about to make our main method async in order to make our Firebase Authentication completely error-free and give it a few nano-seconds to gear up.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Future&amp;lt;void&amp;gt; main() async {&lt;br&gt;
WidgetsFlutterBinding.ensureInitialized();&lt;br&gt;
await Firebase.initializeApp();&lt;br&gt;
runApp(MyApp()); &lt;br&gt;
//MyApp is our root widget.&lt;br&gt;
}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;POINTS TO PONDER WHEN USING PROVIDER&lt;br&gt;
Providers are strictly scoped which means that they turn out to be erroneous when not defined in the right place. Always ensure that you initialize your Providers in the root widget (top of the widget tree to be more precise) so that they remain accessible throughout the nested widget tree and nested navigation routes.&lt;br&gt;
In case you run into a scenario where you don’t want your Provider to be initialized in the Root widget, remember that any attempts to accessing the Provider which you defined in some other route makes that Provider to be inaccessible to its ancestor widgets/Root Widget in the Widget tree. Hence that Provider will only be accessible by its child/descendant widgets.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;Creating our Root Widget (MyApp). As the Providers need to rest on the top of the Widget tree, we will make the MaterialApp widget as the child of the &lt;strong&gt;MultiProvider&lt;/strong&gt; widget to initialize our Providers in the build method.
3.a. Initialize the &lt;strong&gt;AuthenticationProvider&lt;/strong&gt; using Provider()
3.b. Use StreamProvider() to read the value from the authState method in the &lt;strong&gt;AuthenticationProvider&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Now let’s add another class Authenticate to decide which route the application has to take based on the authState.&lt;/li&gt;
&lt;/ol&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Complete &lt;strong&gt;main.dart&lt;/strong&gt; file.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Time to build our &lt;strong&gt;HomePage&lt;/strong&gt; and &lt;strong&gt;LogInPage&lt;/strong&gt; routes. (Make sure you import them in the &lt;strong&gt;main.dart&lt;/strong&gt; file).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;login_page.dart&lt;/strong&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;home_page.dart&lt;/strong&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And that’s a wrap!&lt;br&gt;
This way, we have managed the state with the Provider to keep our users logged in. Hope the article was engaging enough. Still, stuck somewhere? Feel free to check out the &lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/RaajeevChandran" rel="noopener noreferrer"&gt;
        RaajeevChandran
      &lt;/a&gt; / &lt;a href="https://github.com/RaajeevChandran/Firebase-Authentication-in-Flutter-with-Provider" rel="noopener noreferrer"&gt;
        Firebase-Authentication-in-Flutter-with-Provider
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;Firebase Authentication in Flutter with Provider&lt;/h1&gt;

&lt;/div&gt;
&lt;/div&gt;



&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/RaajeevChandran/Firebase-Authentication-in-Flutter-with-Provider" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;
GitHub repository.

&lt;p&gt;ORIGINALLY POSTED IN &lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="https://rajeevchandran618.medium.com/firebase-authentication-and-keeping-users-logged-in-with-provider-in-flutter-f1c66cdb1bc7" class="ltag__link__link" rel="noopener noreferrer"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fv2%2Fresize%3Afill%3A88%3A88%2F1%2A0hhePjY8oMHyTvx-CZPqSw.jpeg" alt="Raajeev Chandran"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://rajeevchandran618.medium.com/firebase-authentication-and-keeping-users-logged-in-with-provider-in-flutter-f1c66cdb1bc7" class="ltag__link__link" rel="noopener noreferrer"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Firebase Authentication and keeping users logged in with Provider in Flutter. | by Raajeev Chandran | Level Up Coding&lt;/h2&gt;
      &lt;h3&gt;Raajeev Chandran ・ &lt;time&gt;Dec 10, 2020&lt;/time&gt; ・ 
      &lt;div class="ltag__link__servicename"&gt;
        &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fmedium-f709f79cf29704f9f4c2a83f950b2964e95007a3e311b77f686915c71574fef2.svg" alt="Medium Logo"&gt;
        rajeevchandran618.Medium
      &lt;/div&gt;
    &lt;/h3&gt;
&lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


</description>
      <category>flutter</category>
      <category>dart</category>
      <category>firebase</category>
      <category>authentication</category>
    </item>
  </channel>
</rss>
