Firebase Phone Authentication Android Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi friends this is Bilal Khan back with a new video in this video I will create a project to demonstrate how we can use firebase phone authentication in our application I have also published a firebase phone earth video earlier but this video is updated and will cover more thanks so let's get started as you can see this is my project and to save time I have already created the design and it is a very simple design you can get it from the github repo of the project the link is given in the description so I have three activities activity main activity profile and activity verify phone ok so this is the first activity where the user will put his phone number so in this activity I have a spinner where I will display a list of all the countries to get the country code and inside this edit text the user will put the phone number and then he will press Continue to verify the phone number and to verify the phone number we will go to this activity and here we will send an OTP or code to verify the phone number using SMS ok and then if the code has verified successfully we will come to activity profile that means the zionists successful so let's start with the main activity where user will put the phone number in the edit text ok and one more thing I also have I have this list of countries and the list of calling codes ok so you can get it from the source code the link is given in the description so don't worry about that so come back to main activity and this is a black activity and the first thing I will do is I will load all the country names into the spinner ok so as you can see this is my spinner and the ideas spinner country so come inside main activity and here we will create a spinner object okay we also have an edit text to get the phone number okay so we have added text and spinner now let's load all the countries in the spinner so first we will get the spinner using find view by ID then we will set an adapter to the spinner using new array adapter and the type is string the first parameter it takes is the context then the layout which is Android dot r dot layout dot simple spinner drop-down item and the last parameter is the item okay so I have this array here in the country data class which is country names so let's pass it here okay so we have contributor dot country names so we have the spinner now let's initialize the edit text as well that's it now we will attach a click listener to the button so we will write find view by ID r dot ID dot button continue dot set on click listener ok now this is called when we click on the button so here we will get the country code and the phone number okay so the first thing we will do is we will verify if the phone number is entered or not okay so let's get the country code first code equals to country data dot country area codes okay this is an array and here we will pass the index of the selected item from the spinner okay so here we will write spinner dot get selected items position okay so we have the code now we will get the phone number that is entered by the user okay so let's get it string number equals to edit text dot cat text got to string dot trip okay then we will check F number is empty then we will set an error to the edit text saying number as required okay and we will request the focus and we will stop the further execution okay now F validation succeeds we will start the verify phone activity okay so let's create an intent and we will go to the verify phone activity class okay we will put the phone number in the intent okay so let's concatenate both the score and number to generate the phone number okay so first we will concatenate a plus then the code and then the number okay and one more thing we need to do as f number is empty or number length is less than ten okay this is correct for India I don't know about the other countries okay so plus code and number and here we can write valid number as required okay so we have the phone number now we will put this phone number in this intent okay so we'll write put extra and we will pass phone number and then the phone number okay and then we will start the activity and we will pass the in time okay so the main activity is complete now we will come to the verify phone activity here first we will get the phone number that we passed with the help of intent okay so that's right strength phone number equals to get intent dot get strings extra and the strength value is phone number okay so people write it here and we have the phone number that is entered by the user with the country code okay now come to verify phone design okay so here we have an edit text where the user will enter the code that is received but we will detect the code automatically okay but sometimes the automatic detection do not work so in that case the user can enter the code manually as well okay and then after entering the code the user will press the sign-in birth button okay so from here we will start the firebase phone orth actually so the first thing we need is we need to add the firebase authentication in our project okay so click on tools and select firebase and this is the firebase assistant and from here select authentication click on email and password and then connect to firebase now from here you can create a new firebase project or you can choose an existing one okay so I will choose an existing project let's select my wallpaper app okay so I will connect to this project so the connection is successful now we need to add firebase authentication to our application so click here and accept the changes now go to firebase console and make sure you go to the account where you have your firebase project in case you have multiple accounts okay so I'm using this account and the projectors my wallpaper app go to authentication and click on sign-in method and here you need to enable the food authentication okay so enable and save the changes so the phone authentication is enabled and it gives you 10,000 verification per month okay for free so it is awesome now come back to your project and you can see the dependency is set up correctly okay so come back to verify phone activity dot Java and here we will create up method okay so let's create a method that will send the verification code so let's create private void send verification code okay and you can name your method anything and it will take the phone number where we need to send the verification code and here we will write phone auth provider dot get instance get instance okay and then we will call a method verify phone number okay so if you need to use phone authentication you have to call this method verify phone number okay and the first parameter it takes as a string and this string is actually actually the phone number where you need to send the verification code okay so we will pass this number for the first argument okay the next parameter is the long value okay and attach that time-out duration okay so after what time you want to resend the code okay so let's put 60 here that means 60 seconds and in the third parameter we define the time unit okay for this time out duration okay so for now I'm using seconds so I will write time unit dot seconds okay then the third parameter is an executor and here we can pass task executors dot mean thread okay then the last parameter we pass as a callback instance so for now let's write here am callback okay and after this we will create a callback instance okay so after this method I will create private phone auth provider dot on verification state changed on verification state change to callbacks okay and then I will name it am callback as I have written here and then equals two now you do not need to write everything just right new press ctrl + space and select this okay and a test done by the IDE okay now here we need to override one more methods so right click here and select override methods and override this method as well okay which is on code set so guys we have three methods here okay this method will be called when the code is sent okay and this string has the verification ID that is sent by the SMS then this method is called when the verification is completed and here we can get the code that that is sent by the SMS automatically okay so in this case if this succeeds user do not have to enter the code man the knee okay and if the verification fails this method will be called so here we will simply display an error message okay so let's make a toast here and we will display the error that has occurred and we can get the error message from this firebase exception object so here we can simply write a dot get message then the length of the toast and then show okay now inside this method on code sent we will store the verification code that does send okay so let's write here private strength verification ID okay we need this string later to verify the code that is entered by the user okay so here we will store verification ID equals to s okay and inside this method we can get the code that is sent by the SMS okay so here we can write strength code equals to phone auth credentials this object dot get SMS KO okay so it will detect the code automatically now sometimes it is null if the auto detection did not work so in that case user have to enter the code manually okay so that's it now we need to attach a click listener to our button which is sign-in button and we need to call this method as well so here we will call the method send verification code and we will pass the phone number okay now write find view by ID r dot ID dot button Simon dot set on click listener now this button is needed when the user has to click on the button by entering the code manually okay if the auto detection work user do not need to click on the Sign In button ok everything will be done automatically so here we will call a method to verify the verification code okay so let's create one more method to verify the code that is detected or entered by the user so we will create private void verify code okay and to and to this method we will pass the code that is entered or received automatically ok now inside this method we will create a phone auth credential object equals to phone auth provider dot get credential okay it takes two parameters the first parameter is the string this one the verification ID and the next parameter is the code that is entered by the user or that is detected automatically ok so the first parameter we will pass this verification ID and for the second parameter we will pass this code okay and then we will call another method to let the user sign n okay so let's call this method sign in with credential and you can again name your method anything and we will pass the credential now to create this method press alt enter and the method is created now we also need a firebase auth object so let's create it here and let's initialize it as well now here we will call the method sign and wet credential okay and it takes credential as a single parameter then we will attach an one complete listener okay so we will write add on complete listener and we will pass the on complete listener so you do not need to write everything just press ctrl space and select let me show you again new control space select this on complete listener okay and it is done by the IDE now here we will check if the task is completed or as successful then we will start the profile activity else we will display the error okay so let's display the error okay so we have the error now if the task is successful we will start the new activity okay which is the profile activity so let's create an intent here intent intent equals to new intent and the first parameter is the verify phone activity brought this the next parameter is the profile activity dot class now we will close everything else and we will start a fresh new activity for this we need to set some flags to the end tank so we will write intent dot set flags and we need two flags the first one as Flag activity new task and the next flag s flag clear top okay so flag clear task actually now what this will do this will clear everything from the stack and will start a new activity by closing every other activity of this application okay so now if the user will press the back button the application will close instead of taking the user back to the login screen okay now we will start the activity okay that's it now we just need to call this verify code method okay so we will call it here as well so if the code is not null that means the code is detected automatically okay so here we were right verify code and they will pass the code okay and one more thing I have in this activity verify phone let me show you I have a progress bar okay so initially I will make the visibility as hidden or gone okay and let's create the progress bar instance as fell so we have private practice par frog the spa and here we will initialize the progress bar progress bar equals to find V by ID r dot ID dot progress bar let me confirm the ID it s progress bar okay correct now we will display the progress bar here okay so I will try it progress bar dot set visibility visible okay and I will call the same thing here as well when they user will click on the button okay so in this case we need to get the code from the edit text and this time it is manually entered by the user okay so here we will OH we forget to create the edit text let's do it private edit text and it text okay let's initialize it we have the added text now get the code from the edit text now you can also attach a validation if the code is empty or the length of the code as less than 6 okay then we will write edit text dot set error enter code and we will request the focus and we will stop the further execution okay if the validation succeeds we will display the progress bar and we will call the verify code method now it should work so let me show you it is working so this is our application now let's select the country and here and I will put my number here let's click on continue you can see that code is received and we are in the profile activity and I have missed a little thing okay so we do not need to display the progress bar here okay so cut it from here but we will display ad when we call da when we call this method send verification code okay so here we will display the progress bar okay and when the code is received we will set the code actually this is not necessary but we will set the code to the edit text okay so with the right set text code okay and now we can also right here inside main activity we can check if the user has already logged in so here we can override one more method override methods and we will override on start and here we will check if firebase auth dot get instance dot get current user if it is not null that means the user s already logged in in this case we will start the profile activity okay so I will copy the same intent code and here we need to write this only this will work okay that's it now if I will run my application again what will happen you can see I am at the profile screen directly okay so to login again I need to log out first okay so let's quote the activity profile come to activity profile and here we have a button and let's put the ID as button logout come to profile activity and here we can simply write find view by ID r dot ID dot button logout dot set onclicklistener and here we can call firebase auth dodd cat instance dot sign out okay and then we can start the new activity okay then we will copy the same thing this intent thing from very five phone activity ok so copy this thing and come to profile activity paste it here okay and this time this is profile activity dot this and we need to start main activity dot class okay so let's test it again so we are at logout let's click on logout you can see we are again at the login screen now let's select the country again put the phone number continue and this time we are seeing the progress bar let's wait for the code you can see we receive the code and the sign-in is done automatically ok so it is working perfect now so that is all for this video friends I hope you found it helpful if you did then please hit on that like button subscribe to my channel and tell your friends to subscribe to thank you guys this is Bilal Khan signing off
Info
Channel: Simplified Coding
Views: 92,920
Rating: undefined out of 5
Keywords: firebase phone authentication example, firebase phone authentication android, firebase phone authentication android tutorial, firebase phone authentication example android, authenticate with firebase on android using a phone number, firebase phone number authentication android, firebase phone number verification, firebase sms authentication android
Id: JZ8hwzBKsMM
Channel Id: undefined
Length: 27min 44sec (1664 seconds)
Published: Tue Jun 12 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.