How to Keep User Logged in Flutter | Firebase Exception Handling Flutter | Flutter Firebase

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back in this video we will be exploring how we can keep our user logged in even after the app is being closed currently we don't have this functionality from the code we have developed in the previous video if you don't know how to login and sign up user with flutter and Firebase I have put a link in description of my video you can go there and watch the video and please make sure you subscribe share and like my channel so now coming back to the main content what we need to do is that if I uh login a user now now in the uh app that we have created in the previous video let's put the credentials here and login so our user is logged in and we have been navigated to the home screen but if I restart my app the user state is not saved and we come back to a login screen again we don't need this we need to keep our user logged in unless he himself logs out so we need to uh develop this functionality um and also we need to uh disable this uh manual navigation to home when the user is logged in and similarly for our sign up as well and in the home screen we also do this manual navigation manual usage signed out so for that what we will do we have this pspec DML file here we have installed this Firebase all dependency uh using this Firebase all dependency we can achieve our purpose of keeping our user logged in so for that what I will do uh here in the lib folder I will create a file known as uh wrapper do dot here it basically wraps our application it is a widget that reps of application here it will return a scaffold and in the body of scaffold I will be using stream Builder the stream Builder uh takes a stream we will use the stream from the Firebase o plugin which is Firebase or. instance. set changes we will use this stream let's remove cons from here what this stream does this stream basically returns a value of user nullable uh whenever the user is logged in this user is not null and whenever U the user is signed out so null is returned here in the user so uh based on these values we can uh put our Logic for login and sign off so we can control our navigation from here whenever our user logs in or logs out so we get a snapshot of value for the user from here so based on that value we can put our Logic for login and sign out so here what I will do uh I have this snapshot returned from the uh stream of user given by FS o and based on this snapshot what I will do I will put some checks like if the snapshot. connection State equals to connection state. waiting which means that the data is not being fetched yet but they're trying to our network is trying to to F the data so here what I will uh return is that Center child and will put a circular progress indicator to show that uh the data uh is being feted or it's in progress U so that's how we handle it and then else if uh the snapshot dot has error which means something if something wents wrong so what we will do we will be returning Center and here we will put a text for intimating that something has uh went wrong or there is some error and then in the lse part when we will have proper data returned so here now we will handle our Logic for uh our logic when our user logs in or sign out so this logic will be uh applied so uh let's uh let's show you how to use this logic if the snapshot. data which is a nullable user uh uh it is uh equals to null which means the user is currently not logged in but signed out so uh here what we will do I will return the login screen because the user is signed out currently because the data is being returned null and um else um if the snapshot data not equals to null which means we have our user logged in so what we will do we will go to the home screen and that's the logic for our uh wrapper here uh in the main screen currently we are moving directly to the login screen here what we will do we will put this wrapper so when the app starts it will go to the wrapper screen and from the wrapper screen uh we will listen to O States authentication States for the user if the user uh returned here is uh not equals to null so we will go to the home screen if it returns uh null which means the user is not logged in so we will go to the logged in screen but if you remember we logged in our user and then we refreshed our app so our user was logged in but we didn't handle that state persistence logic so that uh we were still uh going to the login screen but now our user will be here unless and until we signs out from here but uh with this we also need to remove this manual uh uh navigation logic because we don't need it anymore and that all navigation is being done in the wrapper so let's remove these from these screens we don't need to check it here now also we don't need to store this user as well here and I will remove this uh uh go to home go to sign up will not be removed because it's been used here in the login screen let's remove this U warnings and now in the sign up screen I will also remove this logic we don't need it now and also remove this user variable from here and this warning as well now if I refresh our app so we have implemented uh user persistence logic so um let's signs out and see what happens yes we have removed the manual navigation logic and still we are coming back to the login screen so uh let me log in a user here I will put Khan email.com and I will put Khan 1 2 3 4 here now I will log in and when I restart my app so it will which is the data from the stream and it sees that the user is already logged in so it directly navigates to the uh home screen because our users already logged in okay but if I sign out and then I check uh again restart the app then our user is not logged in and the data returned in the wrapper is null here the stream returns this null value so we goes to this login screen so that's how we persist the state of our user uh with Firebase Authentication oh we have missed one thing and that is here in the sign up screen here after we sign up a user we will use this Navigator poop it is because in the rapper we have the home screen and login screen here whenever the user State changes um we will either move to a login screen or home screen we don't have a signup screen here and we move to the signup screen from the login screen here we have this logic go to sign up so uh we need to come back to this login screen in in order for our function it to work and move from sign up screen to the uh home screen so now if we test here we move to the signup screen and here if I create another user by the name haris and give it a name haris1 12 at email.com and then haris1 so if I sign up it will come back to the login but here in the login uh it will come back to a login screen but here in the wrapper we have uh the user created and he is being logged in because user is automatically logged in when it is created for the first time so when it comes back to the login screen here from the rapper the State updates and he goes to the home screen so from sign up he comes back to the login from loging he goes to the home screen that's how it works but if we remove this thing so the sign up won't work and it will stuck on the same sign up page because it is an other page over the login screen so we need to remove this first in order for a rapper to work uh so that's how uh we manage user user State and one more thing we need to add here in the O service we have some error codes returned by Firebase in case something went wrong so uh we will also handle these using the uh exceptional Handler function so we will use this exception Handler uh method and what it takes it takes a string which is an error code and we will use this error code to show different messages to the user in case something wents wrong so what we will do we will use a switch we will pass this code in the switch and we will handle different cases for different conditions and error codes the first error code is for invalid credentials whenever user enters some uh invalid credentials during login so this uh error code is returned so we will intimate user that um um your login credentials are um credentials are invalid so the user easily understands what went wrong so here we will add two other cases these are some of the common cases we need to handle there are also some other cases that you can look for yourself I put a link in the description you can go there and uh explore the other as well but I will use this common and here we will also use a default Block in case we have an other error code returned that doesn't lie in this three that we are handling so we will um show some random message in that case for weak password uh uh we can intimate that uh your password must be at least eight characters so that's an intimation for password similarly here for email already in use uh it is used when we uh you try to sign up using an email that already exists in the users so uh here we can intimate that user already exists so this is used in a signup and uh this is also used in a signup flow this uh weak password and for an that is using a login so we have handled some common o exceptions for Firebase and the default what we will print is uh something went wrong when um any of these cases uh matches so we have some prompt for them but uh for some exception we can throw some random error that is something went wrong that will help and we have implemented our exception Handler now we need to use it in the proper place here in the tri cage Block in case we uh we have fire base or exception received we can catch that exception and we can take the code from this exception and pass it in the exception Handler we will pass the code from here and it it will take the code and map it to the required uh string message error message that we want to display to the user and our exception has been handled similarly we will use it on the uh login as well so here add this in the login as well for sign out we don't need to add this so that's how we handle different uh authentication exceptions return from Firebase now if I sign out and try out these uh invalid credentials and other scenarios so I will add a random email and some random password in order to check the exception handling so here you can see your login credentials are invalid so we get the information to correct our credentials similarly for sign up let's go to the sign up and here uh let's create a user that will be safey and uh I will use an email safy at email.com but I will use a weak password here which is 1 2 3 4 uh it is not enough so it should throw me the error your password must be at least add characters and it came back to the login screen because of the functionality we have implemented there and similarly we can also use uh an existing email in the signup flow to check whether the error message shown or not uh in case of any exception other than these three this message will be shown that's how you keep your user loged in and also you handle Firebase authentication exceptions that's it for today video see you in the next video goodbye
Info
Channel: CodeWorm
Views: 246
Rating: undefined out of 5
Keywords: keepuserloggedinflutter, firebaseexceptionhandlingflutter, firebaseauth, flutterfirebase, authexceptions
Id: Jvj9q0hNtps
Channel Id: undefined
Length: 13min 11sec (791 seconds)
Published: Sun Mar 31 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.