Login & Register Android App Using Firebase | Android Studio Authentication Tutorials | Part 2/4

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
to connect our app to firebase first we need to go to the tools and click on firebase option and here we need to specify what kind of firebase service we want to use for our app so here we are going to use this authentication using email and password so click on this authentication and you will be presented with this email and password authentication option click on this now here is few option first we need to connect our application to the firebase so click on connect to firebase it will open the browser and here you need to log in with your Google account so guys if you don't have Google account or gmail account created and connect to your firebase now you need to select your account so click on you're logged in account or log into new account now you need to give the access to the firebase click on allow ok it says you have successfully logged in to your firebase AR you have successfully connected your account to an Android studio now if you click on the firebase you will have the option to go to console so it will open the firebase website and you can see in there on top right corner there is one option to go to the console okay so in the console you can control your application you can customize your application and so many things you can do with firebase okay but for now we are only interested in we are not going to do anything in console for now so I'll just go to the end of this to do and now you'll see you have few options to create the new firebase project and you may see here I have already connected to more apps with my firebase account you may not have any right now because you are doing it for the first time so don't worry and just now click on connect to the firebase it will show the dialog here connecting to the firebase it will take few seconds once your app is connected it will display the connected message here and after that need to follow the step two and you need to click on the add dependency to the app and once the dependencies libraries are added to our Android application it will display the dependencies set up correctly okay so I have already done that so it is showing the dependencies are set up correctly so you need to do that and sync the Gradle file okay once the Gradle file it's synced we are good to go okay so I hope you can do that now we are going to implement this register application so that we let user to type the name email password and we're gonna store that to our firebase database so you may see here I have added one more component that I forgot to add that is progress bar so once the user fill in the data and click and register we are going to display some progress bar here so that user user understand that some background process is going on so if it takes more than a few seconds then user won't feel that the UI is freezed or something okay so initially this progress bar will be hidden so let me set the visibility of this so if I go to visibility I can set it to invisible okay so now in if I go to the registers of Java Java I have created a few variables so these are the variables that we required so you go ahead and create in for the simplicity I have already created here so if we see the Edit takes we have full name email password and phone number for button we have register button login bottom button and important is this fire was auth okay so this fire of firebase auth classes provided by the firebase using which we can register the user so that's why we need the instance of this class and we have also created the progress bar variable so below in the oncreate method I have correct our variable to the xml resources so you know the drill we have full name e-mail password phone number button and all the things are connected to our resources on XML file using fine view by ID I hope you can do that so I didn't need to explain anything and here this is the important part so if we see f4 I have articles to firebase authored kate instance so what we are going what we are doing here is we are getting the current instance of the database from the firebase so that we can perform the various operation on the database okay so below this now we are going to write the actual code that are going to that is going to register the user into the firebase okay so the main thing that we need to understand here is that once a user feel the data here in the edit text form field we are going to let the user click on this register button and once the register button is clicked we are going to validate the data okay if the email is entered properly or not the password is less than 6 character or not and so on so to do the validation we are going to need to handle the registry click event ok so we need to handle the click event so that can be handled by using this head click set down click listener so when the register button is clicked we are going to set on click listener new on click listener and inside the only we are going to perform all the operation ok so first thing I'm gonna do here is I'm going to get the value that it has been entered in the email and password field so those both values are string so I'm gonna get that the first one is email and that will be we are going to get that from the email field so email get text and we need to convert that to the string because initially it will be as a object so we need to convert to the string and I will trim the data okay just to format the data so string second thing we need is password from the password field so I am password to cut that gate text the to string dot trim okay so now we need to validate the data that has been saved in email and password so first thing we need to say is if the value is entered or not if it is null or that the email field is empty then we are not going to let the user register the account okay we need that value because it is required right so to check if it is empty or not we need to use one more class called pick utility utils okay and it has one method called empty is empty so inside this I'm going to pass the email string and if this is true that means user have not entered any value in the email field okay so I'm gonna display the error to the user that the email field is required so we can set that by using email variable okay and seitaro matter so I'll say email is required okay and I'm gonna return this if we are also going to take the same thing for the password as well so if the password is empty we'll display the error to the user okay so I'll pass the password here and AM password field set error and I'll say password is required similarly I said that we must use password more than 6 character long ok so I'm gonna take the length of the password is greater than 6 sorry less than 6 then I'm gonna display the error to the user that password must have six or more character long okay so password must be greater or equals to 6 character characters okay so these are the error that we are going to display to the user so if all of these condition is passed that means the data are valid and we can actually start registering the user okay so right now here I'm gonna display the spinner to the user at a progress bar letting you know the user that we have started the process of registration okay so progress bar that set visibility remember that we have made this progress bar invisible in the UI so I'm gonna make this visible here we can use view dot visible method or properties okay now we'll register the user okay so for that we need to use fireballs firebase object instance so and we need to use create user with email and password method so here you need to pass two things first is username or email and second is password now we need to add some event listener so that we will get to know if the registration is successful or not okay for that we need to use add come on complete listener method and inside this you need to create new on complete listener and here we'll check if the registration is successful or not so the process of registering the user is called as tasks here so if the task is successful that means we have successfully created the user if not then we have encountered some error so what we can do is we can display that arrow to the user so if the account is created I'm gonna display that user is created to the user and I'm gonna redirect user to the main activity if not I'm gonna display what whatever the arrow has occurred to the user right so we can use ifelse condition here so if task is successful that means we have created the user successfully otherwise some error occurred so inside this inside this I'm gonna this first display the message to the user that we have successfully created the user so user created okay and I'm going to send the user to the main activity so to do that we need to start the new activity inside this we need new intent from the current app context I'm gonna descend user to the main activity like this okay otherwise I'm I'll displays message to the user saying that some error is occurred and I'm gonna attach the exception that we get with Ag masses okay error message okay so this will handle the user creation but before doing this we need what we need to do is we need to check if the user is already logged in or not okay so if the user is already logged in or he has already created the account before and coming second time or returning user then we need to send them to the main activity so there is no point of displaying register or login form to the user who is already logged in okay so that can be checked by using a special method in our F or firebase South class so let me use a effort dot get current user so we are getting the current user object if the current user object is present that means user is already logged in so we don't need to display anything to them we'll just simply send them to the main activity so if the current user is not null then we'll send them to the main activity so to do that I'll just copy this from here paste it here and finish it okay yeah so this will handle all the condition that might occur in the registration process so now we can actually run the device around this app and see if it is working or not so let me connect my device first okay the my device is connected and it is displaying here but before actually running this into my device what I'm gonna do is I'm going to set the user's permission here so we need to have the internet usage permission from the devices so let me say that Internet okay now we are we our application can use the internet from the device fire-based at google.com right so let me go to console from here okay so this is our app project overview and inside this sign-in method okay in the develop authentication inside the sign-in method we need to enable this email and password so initially it will be in disable State okay just click on this enable button and click on save now we can actually start saving the user in to the firebase database so I can run this app now so let me run this and see if he is working or not okay if we have program everything correctly here it should work if not we'll figure it out okay so the app is running okay now the app is running and now let me simply click on register and see what happens so you can see some error is displayed in the form email is required okay so let me register to this app and see if we is working or not so I'll use some name and email so okay I'll give the password okay I'll just give the password of three character and see if it is checking the validation or not okay so now if I click on register it should it should display the error yeah and it says the password must be greater or equals to 6 characters okay that is working fine so let me make the pass for 6 character and click and register and I will see what happens so you can see the spinner is running and you can see the user created message and we're actually on the main activity so in the mainactivity I have given this message welcome to the dashboard and some love our button is given so if I go to activity main and display the preview you can see this takes pew and this button so in this button I have given one on click method okay and I have created that method to the main activity so inside main my main activity I have created this method called lock out which is our unclick name and if you click here and click on on click event handler or something you know otherwise I'll just show you what to do so I have just changed it and now if I click on this red button it will give the option to create logout on view so you just go ahead and create the log out log out method in the main activity and inside these we need to call these gate instance that sign out method it will do the log out of the user that is currently logged into our application and once the user is loved out I'm sending them to the log in class sorry login activity so that's why we started new activity here from the current class I'm sending them to the log in class ok so let me show you what happens if I click on logout in my application so let me go to the app and if I click on logout they are redirect to thee email login activity ok so that's what this start activity is doing here and this our sign out method okay so so that that is how then that's how we register the user using the firebase now I will implement the login activity and we'll let the user login using the right username and password ok so if I show you the activity log in XML file here also you I have added one progress bar that I might not have included while creating this UI so if I say to the visible you can see the progress bar okay so you just go ahead and add this progress bar here and said that to the invisible because initially we are not displaying the progress bar right so inside the login activity I have creel ready created a few variable so for edit X we have email and password for login I have login button text view create button progress bar and firebase Earth okay so these are the variable we need to implement the login activity and as you know we need to connect this to the XML resources so that's what I have done here so just like in register option once the user click on register we were doing all the validation and we were letting user to register in the database simply like that we are going to do the same thing here we are going to validate the users input once the user click on log in and we are going to take the email and password to the firebase database and if the information is correct we are going to let them go to the main activity so so first thing I'm going to do is I'm going to implement the state onclicklistener in our login button so say to onclicklistener new onclicklistener okay so inside the on click i'm going to copy and paste same thing from the register class so these are the same thing we are going to get the email and password field and we are going to validate if it is correct or not and we're gonna also going to validate the length of the password so I better copy this from the here there to here for the simplicity and import the class so this is the same thing now we are going to authenticate the user so to authenticate the user we need to use this email and password so I am using this a fourth class object and I'm gonna use the sign-in as I sign in with email and password method of the firebase so inside this I'm the I need to pass two arguments that is email and password and I need one event handler so I'll add the add on complete listener and I'll create new uncomplete listener and inside this we need to check if the login is successful or not okay so this task will determine if the login is successful or not so if task is successful then we have successfully logged into logged in the user so I'll copy the same message from here and instead of displaying user is created I'll say and I need to change this to the current activity that is login okay and instead of saying user is created I'm gonna say logged in successfully yeah so this is how we are going to display the twist message to the user when he's successfully logged into the application and we are redirecting them to the main activity if not then some Aurora's occurred right it might be email is not right it might be password is not correct or password is you know incorrect so whatever the error message that I'm going to display okay so for that I can use the same toast message so instead of writing that I'm gonna copy from here ctrl C and I'm gonna paste it to thee and I'll change this current activity to login okay so this is how we do it now if I rerun the app we must be able to log into our application so let me run the app okay so let me go to the register okay so this textview is not working if I click on this I am NOT going to the plug-in activity so let me fix that quickly so in the register dot Java I have created one separate variable for that textview that was called am login button so I'm going to display the onclicklistener on that button on that textview so whenever we click on that will be redirecting to the login activity so I'll set set on click listener new onclicklistener an onclick I'm going to start the activity that will go to the login activity so let me start that and I'll send them to the login activity okay so similarly in the login button if I show you the login UI file here we have create a context view right so whenever we click on this create account textview we should be able to go to register activity so I'm gonna say that as well so if I go here and here I can see em register button is there so I'm going to set the onclick listener on that sorry not in this in here so here it is called create button okay so I'm gonna set the same thing so better I copy from here so from register to here I'm gonna paste that and instead of this login button I'll use the create button right so let me paste that and I'm gonna go to register activity register class okay so this should enable the textview on both login and register activity we'll see that once the app is running so now if I click on this I'm redirected to the login activity and if I click on create account I'm going back to the register activity right so I'm going to is I'm going to display if this our email and password sorry if this login activity is working or not let me insert my email that I use to resistor so okay and I'll set the password I think that was one two three one two three okay now I'll click on login and see if it is working or not okay the login successful message is displayed and I'm on the main activity right now if I click on logout I should be able to go back to the login activity so let me take by putting wrong email instead of right email so I'll say in B Costard similar calm and I'll put some password okay let me put the password that is not six character and what happens so it will say password must be six character long and if I put the some password that is more than six character and click on login then I will see what happens so it says there is no user okay and even and it says there is no user and this I spinner keep on running so it should get hidden once the error message is displayed okay so I'll fix that by setting that inside this else method of our effort assign in an e-mail password method so I'll make the progress bar gone and I'll copy and paste the same thing in register activity after these else okay I'll paste it here and I'll save it and I'll rerun it and we'll see it again if it is hiding the progress bar or not okay let me go to the login and I will put the right email and wrong password and we will see what message it is spread here so I click on login it will see the password is invalid or the user do not have password and the spinner is gone right now I'll say the right password and I should be able to login yeah and now if I go to the firebase console and so you will have the all the users that I have registered here so let me go to the firebase firebase to google.com right so let me go to console from here okay so this is our app and go to the authentication I should be able to see all the users that have registered using the our register activity so these are the user I have already registered using this okay so you can rephrase from here you can edit delete and here you can see we are just using email and password for the our database okay the full name and phone number is not restored here so to do that we need to create the unit we need to use the database okay so that is more advanced so let me know if you guys want me to create more advanced tutorials on these firebase because there is so many things we can implement using the firebase so lot of application is using this firebase to implement a lot of complex feature in the application so let me know if you guys want me to create that how to create and you know change the data into the user profile how to create the user profile add and add the data remove the data and so on so this comment in the comment box so that I will get to know if you want me to create the tutorials on that so that's it guys this is how we implement the authentication system in Android application using firebase so I hope you guys learned something today and have fun I don't know my videos are not that much fun because I'm more static rather than dynamic so I'm still learning to record the videos for you guys so let me know if you want any more help on these and you can subscribe and like the video so that I can actually know that I'm helping you guys and yeah thank you thank you for watching I hope to see you in my next videos
Info
Channel: SmallAcademy
Views: 239,716
Rating: undefined out of 5
Keywords: firebase phone authentication android tutorial, firebase android authentication tutorial, android studio tutorial - firebase authentication (2019 version), #1 firebase phone authentication android tutorial - sign in, Login & Register Using Firebase, Android Studio Authentication Tutorials, login and register android studio, login and register android studio firebase, firebase authentication android, firebase authentication tutorial, android studio tutorial for beginners, firebaseauth
Id: TwHmrZxiPA8
Channel Id: undefined
Length: 31min 3sec (1863 seconds)
Published: Fri Sep 27 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.