Authenticating Guest Users for Firebase using Dart's Flutter Framework

Tensor Programming · Beginner ·🛠️ AI Tools & Apps ·8y ago

Key Takeaways

The video demonstrates authenticating guest users for Firebase using Dart's Flutter Framework, enabling anonymous sign-in, and using Firebase Authentication plugin and Dart async library. It also covers creating routes for login and home pages, associating widgets with routes using route map, and navigating between pages in a Flutter app.

Full Transcript

hi guys welcome to another flutter tutorial video my name is tensor today we're going to be talking about how you can authenticate a user into firebase specifically we're going to be talking about how you can authenticate a user as an anonymous user into firebase in a way that they can manipulate the firebase data if you've set up the actual database to be locked so that only specific users can access the data now keep in mind there are other ways you can log in a user you have the ability to log in users using a Google account using a Facebook account a Twitter account you can have them log in using passwords and usernames that you create inside of your firebase account and these are things that we may look at later in other tutorials it may seem a little weird that we're only going over how we can log in users anonymously however this is a pretty useful feature because it gives us the ability to authenticate a user and then track how they use our database in firebase so what we can do is have local authentication inside of our firebase application and then we can put the users and password hashes into a firebase store and then we can make it so that when the user logs in quote-unquote anonymously it lines up with their user username inside of the firebase store and then of course when they are logged into our firebase store we can have them do read and writes on specific tables alright so let's set up our firebase project here we are with a fresh firebase project if you guys want to know how to actually set this up so that it works with flutter you should go check out my firebase flutter tutorial for more detail on that in this tutorial we're just going to modify one little thing in our firebase application so that we can allow for authentication so we go to this authentication page and you'll see something that looks a bit like what you see on my screen and then you can click this setup sign-in method and this will open up to a page that will give you all of the different providers that you can choose from you can use email past phone Google Play games Facebook Twitter github and anonymous in this tutorial we're going to be focusing specifically on anonymous so let's click on anonymous and let's click this slider to enable it and then we'll click Save and this will enable the anonymous sign in in our application now we can jump back into our code specifically into our pub spec yamo and we want to add firebase off and at the time of this recording the version is 0.5 point 3 I've got some boilerplate set up already inside of our application we have a simple root widget that creates a material application this points to a stateful widget called login page and then the login page has login state the build function only creating a empty scaffold and then we also have a stateless widget called home page which creates an empty scaffold the idea behind this application is that we want to be able to have the user go to the login page click a button to sign in and then that will take them to the home page we want to make two imports we need the firebase authentication plugin and we also need the dart async library now because we want to use routes in this application we can add these static final strings called route to both of our main pages so one for our login page and one for our home page and we'll give each of the routes a different name so our login page will just be login - page and then our home page will be home - page and this will make it very easy for us to associate these widgets with their respective routes we can then create a route map up here and the map for the routes property is typically a string key and then a widget builder value so we say login page route which is the login - page string and we associate that with a function that takes in the build context and then inflates our login Paige widget we can do the same for our homepage now down inside of our material application we can add our route variable to our routes property so this is something that we haven't really done before but you can have both a home page and have routes in your application alright so now let's build out our login page so we want to get an instance of our firebase auth object this is the object that's going to allow us to login a user as well as log out the user and of course we need this to be singleton so we just take out a single instance of this object now let's create a function called sign in and on and this will be the function that we use to actually sign our user in this will pass back a future with a firebase user inside of it and of course because it's passing back a future we need it to be asynchronous first we'll create our firebase user by setting this equal to or wait firebase off dot sign-in anonymously this method sign in anonymously will then create an anonymous user and authenticate him versus our firebase store and then we can use that user in other parts of our application so I'm going to print out signed in and then we'll take the user and we'll get the unique user ID for this user and print it out and then we'll return our user from this function let's create a function that will sign us out and this function is fairly basic we just call our firebase auth instance and then we call the method sign out on it and this will sign out our anonymous user and I'll just have this print out signed out so that we know that we've signed out the user before we build out the user interface for our login page let's come down to our home page and make it so that we can actually pass a firebase user into this home page so when we get back the firebase user after they've logged in we want to then push to the home page but we also want to keep all of the data that we're getting from the login of the user and then be able to display it on the home page so what we'll do is we'll create a final firebase user and then we'll take our home page constructor and pass that user in as an optional property now for our user interface we want to have an app bar and the app bar will just say login page then the body will have a center with a ListView inside of it we'll set shrink-wrap equal to true because we want our ListView to expand across the entire page but we don't want it to scroll then we'll give it some padding so we'll just say edge in sets not all 10.0 and then the children inside of this ListView will be the buttons that we create to allow us to log in and log out just so that we don't have a lot of nesting will create our buttons above where we have our return statement so the log in button will be a container and the logout button will also be a container or our a log in button our container will have some padding and then the child for this container will be a material widget a material widget is quite literally a piece of material in other words it's like a container except it has properties that allow us to manipulate it in a way that follows the material design specifications for our piece of material we want to set up the border radius will have this be border Li radius circular so that it's still a rectangle but it has circular edges so it'll be 30 point-0 then we'll have a shadow color which will be colors deep orange then we'll have an elevation which will make it so that this is slightly elevated off the background and we'll give this ten point zero and then the child of this material will be a material button we want our material button to have a specific width which will be 150 point zero and then a height which will be fifty point zero so our material button will be at least 150 by 50 and then the color of this button will be colors orange and there will be some text on it which will say login as guest the unpressed event for this button will call our sign-in anon function and on our sign-in a non function we can call the then method which allows us to unwrap the future that's being passed back from our function and then take the object that's inside of the future and then put it into an anonymous callback function so here's our callback function we're taking our firebase user from our signed in a non function and then inside of this callback function we're calling the navigator object and then we're calling the of method on that and then we're pushing in a new material route so that we can then go to the home page inside of the push method we can then create a new material page route and then for the Builder property we pass in a function that takes in our build context and outputs our home page and in here we can then pass in the user that we got from our sign-in a non function so this will then pass it to the home page and then open up the home page we can also add some error checking if we want to so we just call catch error on our callback function and then we take the error and we put it into a print statement down inside of our ListView we can now put our login button into the children property now let's build out our logout button we want this to be consistent with our login button so we'll give it the same padding and then the child for this will not be a material button or a piece of material or rather just a flat button our flat button will have colors colors white because our theme is the theme data dark which means the background will be black and then we want to have text on it that says sign out and the style for this text will be colors black so we'll have a white button with black text and then our on pressed function we'll call our sign out function which will then sign out the user from this application and like with our login button we can now add our logout button to our list view now let's build out our homepage in the scaffold we'll just give it an app bar that just says homepage then for the body we'll have a center with a column in it and the column will have main axis alignment of main axis alignment out Center so that everything is centered and then inside of our column we'll have a bunch of text fields and each one will have a property of our user so the first one will be our user data UID then we'll have our user display name and then we'll check to see if the user is anonymous which is just a boolean so this will pass back true or false based on whether or not the user is anonymous all right so now we can build out this application and take a look at what it looks like here we have our two buttons and you can see this one has a bit of a material design look to it and it has a nice little shadow on it as well and a little bit of elevation if we click in login as guest this will then log us in and you can see here is the user unique user ID and then the display name will pass back as null because there is no display name and then of course the user is anonymous so this passes back as true if we come in to our firebase console you can see now that the user actually was created and they logged into our firebase application so here we have that user ID that's appearing inside of our flutter' application and then we have dates for when they were created and when they signed in as well as the provider which in this case is that person which means that it's anonymous and then the identifier which again is anonymous now if we go back into our application and we head back we can sign out let me just click the sign out button and this will sign out the user so now when I click login again it should give us a new and if we reload this page you'll see now there is a second user as well anyway guys I hope you enjoyed this tutorial if you did feel free to like and subscribe if you have any questions or comments feel free to leave them in the box below and if you dislike this video then by all means download it as much as you like have a good day

Original Description

In this tutorial, we look at how we can authenticate users anonymously in Flutter using firebase. Source Code: https://github.com/tensor-programming/firebase_anon_auth_example Feel free to donate: Patreon: https://www.patreon.com/tensor_programming ETH: 0x03247265dd5242605bD2FA3c40fb3b70d9e3D685
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tensor Programming · Tensor Programming · 0 of 60

← Previous Next →
1 NodeJs, Text editors and IDEs
NodeJs, Text editors and IDEs
Tensor Programming
2 Vanilla JS todo App
Vanilla JS todo App
Tensor Programming
3 Elm Tutorial part 1
Elm Tutorial part 1
Tensor Programming
4 Elm Lang Tutorial, Part 2
Elm Lang Tutorial, Part 2
Tensor Programming
5 Elm Tutorial Part 3
Elm Tutorial Part 3
Tensor Programming
6 Elm Tutorial Part 4 -- Analog Clock App
Elm Tutorial Part 4 -- Analog Clock App
Tensor Programming
7 Elm Tutorial part 5 -- Snake Game
Elm Tutorial part 5 -- Snake Game
Tensor Programming
8 Elm Tutorial part 6 -- Calculator
Elm Tutorial part 6 -- Calculator
Tensor Programming
9 Go Tutorial part 1 -- Hello World and Static File Server
Go Tutorial part 1 -- Hello World and Static File Server
Tensor Programming
10 Go Tutorial part 2 -- Web Crawler
Go Tutorial part 2 -- Web Crawler
Tensor Programming
11 Go Tutorial Part 3 (Web App part 1)
Go Tutorial Part 3 (Web App part 1)
Tensor Programming
12 Go tutorial Part 4 (Web tutorial part 2) - Using templates
Go tutorial Part 4 (Web tutorial part 2) - Using templates
Tensor Programming
13 Go tutorial part 5 (web app part 3)
Go tutorial part 5 (web app part 3)
Tensor Programming
14 Go tutorial part 6 (webapp part 4)
Go tutorial part 6 (webapp part 4)
Tensor Programming
15 Go tutorial part 7 (web app part 5)
Go tutorial part 7 (web app part 5)
Tensor Programming
16 Go tutorial part 8 (Web app part 6)
Go tutorial part 8 (Web app part 6)
Tensor Programming
17 Go tutorial Part 9 (web tutorial part 7)
Go tutorial Part 9 (web tutorial part 7)
Tensor Programming
18 Go tutorial Part 10 (web app part 8)
Go tutorial Part 10 (web app part 8)
Tensor Programming
19 Go tutorial Part 11 (Web app Part 9)
Go tutorial Part 11 (Web app Part 9)
Tensor Programming
20 Go Tutorial Part 12 (Web app Part 10)
Go Tutorial Part 12 (Web app Part 10)
Tensor Programming
21 Go Tutorial Part 13 (Web app Part 11)
Go Tutorial Part 13 (Web app Part 11)
Tensor Programming
22 Looking at Elm 0.18
Looking at Elm 0.18
Tensor Programming
23 Go tutorial Part 14 (Web tutorial part 12)
Go tutorial Part 14 (Web tutorial part 12)
Tensor Programming
24 Go tutorial Part 15 (Web tutorial part 13)
Go tutorial Part 15 (Web tutorial part 13)
Tensor Programming
25 Go tutorial part 16 (web app part 14)
Go tutorial part 16 (web app part 14)
Tensor Programming
26 Elm Tutorial Part 7 (SPA part 1)
Elm Tutorial Part 7 (SPA part 1)
Tensor Programming
27 Elm Tutorial Part 8 (SPA Part 2)
Elm Tutorial Part 8 (SPA Part 2)
Tensor Programming
28 Electron Elm Tutorial
Electron Elm Tutorial
Tensor Programming
29 Go tutorial part 17 (web app part 15)
Go tutorial part 17 (web app part 15)
Tensor Programming
30 Up and Coming Programming Languages and Technologies for 2017
Up and Coming Programming Languages and Technologies for 2017
Tensor Programming
31 elixir tutorial part 1
elixir tutorial part 1
Tensor Programming
32 elixir tutorial part 2
elixir tutorial part 2
Tensor Programming
33 Elixir tutorial Part 3 (GenServer and Supervisor)
Elixir tutorial Part 3 (GenServer and Supervisor)
Tensor Programming
34 Elixir Tutorial Part 4 (GenStage)
Elixir Tutorial Part 4 (GenStage)
Tensor Programming
35 Elixir Tutorial Part 5 (Plug and Cowboy)
Elixir Tutorial Part 5 (Plug and Cowboy)
Tensor Programming
36 Phoenix Framework Tutorial Part 1 (elixir part 6)
Phoenix Framework Tutorial Part 1 (elixir part 6)
Tensor Programming
37 Phoenix Framework Tutorial Part 2  (elixir part 7)
Phoenix Framework Tutorial Part 2 (elixir part 7)
Tensor Programming
38 Phoenix Framework Tutorial Part 3 (elixir part 8)
Phoenix Framework Tutorial Part 3 (elixir part 8)
Tensor Programming
39 A Intro to Clojure and Clojure Syntax
A Intro to Clojure and Clojure Syntax
Tensor Programming
40 An Update about the channel
An Update about the channel
Tensor Programming
41 Intro to Rustlang (Setup and Primitives)
Intro to Rustlang (Setup and Primitives)
Tensor Programming
42 Intro to Rustlang (Strings, Tuples, Arrays, Slices and Pretty Printing)
Intro to Rustlang (Strings, Tuples, Arrays, Slices and Pretty Printing)
Tensor Programming
43 Intro to Rustlang (Ownership and Borrowing)
Intro to Rustlang (Ownership and Borrowing)
Tensor Programming
44 Intro to Rustlang (Structs, Methods, Functions, Related Functions and the Display/Debug Traits)
Intro to Rustlang (Structs, Methods, Functions, Related Functions and the Display/Debug Traits)
Tensor Programming
45 Intro to Rustlang (Control Flow, Conditionals and Pattern Matching)
Intro to Rustlang (Control Flow, Conditionals and Pattern Matching)
Tensor Programming
46 Intro to RustLang (Enums and Options)
Intro to RustLang (Enums and Options)
Tensor Programming
47 Intro to Rustlang (Vectors, HashMaps, Casting, If-Let, While-Let, and the Result Enum)
Intro to Rustlang (Vectors, HashMaps, Casting, If-Let, While-Let, and the Result Enum)
Tensor Programming
48 Rustlang Project: Snake Game
Rustlang Project: Snake Game
Tensor Programming
49 Intro to Rustlang (Traits and Generic Types)
Intro to Rustlang (Traits and Generic Types)
Tensor Programming
50 Intro to Rust-lang (Closures, the Box Pointer and Iterators)
Intro to Rust-lang (Closures, the Box Pointer and Iterators)
Tensor Programming
51 Intro to Rust-lang (Modules and Lifetimes)
Intro to Rust-lang (Modules and Lifetimes)
Tensor Programming
52 Intro to Rust-lang (Macros and Metaprogramming)
Intro to Rust-lang (Macros and Metaprogramming)
Tensor Programming
53 Intro to Rust-lang (Error Handling)
Intro to Rust-lang (Error Handling)
Tensor Programming
54 Intro to Rust-lang (Concurrency, Threads, Channels, Mutex and Arc)
Intro to Rust-lang (Concurrency, Threads, Channels, Mutex and Arc)
Tensor Programming
55 Intro to Rust-lang (Tests, Attributes, Configuration and Conditional compilation)
Intro to Rust-lang (Tests, Attributes, Configuration and Conditional compilation)
Tensor Programming
56 Rustlang Project: Port Sniffer CLI
Rustlang Project: Port Sniffer CLI
Tensor Programming
57 Rustlang Project: Chat Application
Rustlang Project: Chat Application
Tensor Programming
58 Rustlang Project: CLI Toy Blockchain
Rustlang Project: CLI Toy Blockchain
Tensor Programming
59 Intro to Rust-lang (Setting up a Development Environment)
Intro to Rust-lang (Setting up a Development Environment)
Tensor Programming
60 Intro to Rust-lang (Building a Web API with Iron)
Intro to Rust-lang (Building a Web API with Iron)
Tensor Programming

This video teaches how to authenticate guest users for Firebase using Dart's Flutter Framework, enabling anonymous sign-in and using Firebase Authentication plugin and Dart async library. It covers creating routes for login and home pages, associating widgets with routes using route map, and navigating between pages in a Flutter app. By following this tutorial, you can build a Flutter app with secure authentication and user management.

Key Takeaways
  1. Enable anonymous sign-in for Firebase
  2. Add Firebase Authentication plugin and Dart async library to pubspec.yaml
  3. Create routes for login and home pages
  4. Associate widgets with routes using route map
  5. Create a function to sign in a user anonymously
  6. Create a function to sign out a user
  7. Pass a Firebase user into the home page constructor
  8. Build a login button with a material design look and shadow
  9. Configure the application to check if the user is anonymous and display their data
💡 The video demonstrates how to use Firebase Authentication plugin and Dart async library to enable anonymous sign-in for a Flutter app, and how to navigate between pages in a Flutter app using a route map.

Related Reads

📰
AI For Time Management: How Artificial Intelligence Can Help You Get More Done Every Day
Learn how AI can optimize your daily schedule and boost productivity
Medium · ChatGPT
📰
I Built a Tool With AI That I Actually Use Every Week — Here’s What “Built With AI” Actually Looks…
Learn how to build a practical AI-powered tool for automating tasks, such as keeping OS images current, and apply it to real-world scenarios
Medium · DevOps
📰
AI-Ready Infrastructure: A Checklist Before You Scale
Ensure your infrastructure is AI-ready by checking key components before scaling
Dev.to AI
📰
How I Cut My OpenAI Bill by 97% — The Full Migration Guide
Learn how to cut your OpenAI bill by 97% with a full migration guide, optimizing costs and improving efficiency
Dev.to · rarenode
Up next
💑 Best Budgeting App for Couples - 🤢Avoid the Couple Money Fights!
Modest Money - Reviews & Coupons
Watch →