Android Kotlin Firebase Authentication Tutorial 2020 | Registration, Login, Logout, Session, Profile

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends in today's video i'm going to implement firebase authentication so i'm going to show you how we can register with the email and password and then we will use those credentials to login and then i'll implement logout so let's get started with the developer console we need to register our application first so simply you can search by the firebase authentication and then click on go to console then you need to log in with the email id and click on add project here you can give a name any name or whatever your application name have then simply click on it i don't need a analytics it will take some time to build a project so this is the basic steps click on android icon here we need to give the same as whatever application is going to have the package name now download this google service json file now we need that app file in our project so inside the app simply that's done now we need to go and go to authentication and enable our email and password authentication just simply enable it and the real-time database also we are going to use so let's create it so in this we are going to hold some information like the profile information like first name last name so that's what the reason you need to have and the rules we need to check everything look good to me so we don't have any account currently so let's start with so go to tools firebase and authentication and click on this link add the dependencies so these are the dependencies which we need to have in our gradle file let it be sync now and we need one more dependency for the database so let's add that too the google one we just need to add the google one let it be sync now all right now let's add some activities no we need one login activity we need another activity for registration and let's rename this main activity to profile activity where we are going to show the username email address and first name last name after the login first of all we need to add our internet to permission in our manifest file also we need to change our launcher as a login activity so let's change it there you go now let's go and build our ui so login screen is going to have just email address edit text and password edit text and the login button and i'm going to add one text view which is going to open the registration link if someone don't have that account so they can click on register here and registration screen will be open [Music] let's copy it and build the registration and ui as well this station ui is going to have just the first name last name email and password and the registration registered button and in profile i'm going to just have few text views which is going to just display the information which we are going to retrieve from the real-time database so like first name last name and i think email so our ui is ready now let's go to our registration activity and create a separate function let's handle the button click on the register button we need to call the firebase api i am declaring the variable now and one button click we just need to call this function create user with email and password but before that let's add some validations so the basic empty validation like email first name last name should not be empty [Music] if it is empty then it will show just this error [Music] validations are done so now let's see how we can call our authentication api to create a user so we just need to pass our user email and the password [Music] and a listener on add-on complete listener inside it will check if it is successful that means our user has successfully registered so we'll write code inside if block so there we can describe the current [Music] user so i'm creating two more variables for database reference that is for the real-time database reference and itself database reference and here we need to give a table name we can say if it is not successful then we just need to show a toast so when registration is success then we need to save first name and last name that is going to inside the profile table so this is corresponding to the user id which is already registered so corresponding to that user id i'm going to store first name and last name in real time database this is the key so you can use any name but make sure that name should be same as what you are going to use on the profile while retrieving the data similarly for the last name i'll just change the key and if it is success then we'll just show a task registration success that's it and now we need to close this activity and we need to call this register function from the oncreate now go to login activity and write the code for logins i'm copying this auth and firebase what we need to initialize in oncreate i'm creating a separate function for login same as register and what are the ids we gave so login id from that button we need to click listener so that is the sign in login button listener i'm putting the validation like the empty check for username and password same as we did in the registration okay validation are done now we need to call our auth firebase dot sign in with email and password this one and pass over email which user entered and password now we need to add a listener on complete listener and here if user will be successfully authenticated then it will come as it dot is successful if authentication get failed then it will come in the else block so we'll show again text message to the user and once authentication is success then we need to send user to the profile activity so let's write the code for that and now we'll close current login activity so that if user press the back button he don't come back to the login screen and now we need to call this login function from oncreate on profile activity we need to retrieve the data so we will be driving first name last name and email so it's the same way as we save similarly we need to redrive so i'm copying the same code from registration activity so we need to initialize database auth and references and i'm creating a fun separate function as usual so firebase op will return as the current user current logged in user and same as dot child user dot uid we need to add a listener and then we need to override its functions one data change and on cancel so we need to handle this on data change this is the first name we are setting on the text view here we need to pass the same id which we pass at the time of registration that is the first name in all small similarly for the last name text view let's copy the id last name dot text pass the same id last name dot values and we need email id as well that we can directly get from the user so current user dot email return us the email id of logged in user so let's append some text like first name in front of what we are going to see last name and email and i'm adding one more button for sign out so i'll show you how we can log as well and i'll show you how you can see the session like user don't need to again and again enter their credential until they are authenticated so i'm handling the logout button dot sign out there and then we need to redirect user to login activity so when we log out that time we need to redirect user to logout activity i mean login activity and we simply close this activity let's run and see here we go so we can see the login screen and let's click on registration oh i think we did not handle it click on login activity yeah let's and let's click as well you can write code in login function itself dot set on click listener and on registration link we simply need to start uh registration activity and we don't need to close current activity because on back leg user can come back to the login screen that's it let's run and see well there you go let's enter some data actually gmail.com and password click on register register success there you go let's try to login with the same credentials login there you go we are successfully able to retrieve the profile data awesome now let me show you how we can see the session so currently it's taking me to the login screen but if we maintain the session then it should not be coming so till the time user is logged in it will directly take you to the profile screen so let's write some code here current user you can always check the current user property if user is already logged in then we can directly send user to profile activity there you go let's run it and see yes so now you see user is directly land on a profile activity one more time and i'm going to log out and then closing this app and then see wizard came onto the login screen one more time and login with the same credentials now again now you can see i'm closing and user is landed on same activity now i'm killing this app and still user can come to the profile activity there after logout user won't come on the profile activity land on login activity that's the beauty of firebase authentication now let's verify the data which we entered so we can see the user and in the real time we can see the first name and last name which we retry so that's all for today's video thank you so much for watching please don't forget to like subscribe and share thank you
Info
Channel: LearningWorldz
Views: 9,967
Rating: 4.8153844 out of 5
Keywords:
Id: lS3EOKshtyM
Channel Id: undefined
Length: 28min 6sec (1686 seconds)
Published: Sun Sep 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.