Firebase Phone Authentication Android - Firebase OTP Verification Android - Firebase Authentication

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
assalamualaikum my name is tae Moo Sikandar and today we are going to create the OTP tutorial which is the phone verification tutorial in android should you are using firebase I was getting a lot of requests to create this tutorial and I delayed that a lot sorry about that and finally let's quickly have a look what we will be creating so open the application as you can see we have created these all things in our previous tutorials and we are keep moving forward then we have removed to the signup screen and in here let's quickly write some details dummy details and into the phone number and then we have a password which should be strong ok when we click on this Go button let's wait for a few seconds for code to arrive the code has been arrived and you can see our loader was loading and we have been automatically moved toward our new screen so this is what we will be creating today ok so let's get started ok before getting started first of all you can see we have already created our signup screen in which user will enter the phone number here and click on this ko button then we need to create another activity which is called verify form numbers and in verify phone number I already designed this activity in which we have the OTP filled user will enter the OTP in case if it is not registered automatically and click on this verify button and then we have a progress bar there are many persons saying that how to use this progress bar so get in the text section and click on this progress bar you can see I have created this progress bar with the ID which is progress in a school bar and you can get this whole code of this design inside the description or visit the music and accomm as I am getting a lot of Curie's to create this OTP video because it is a bit complex so for therefore you guys what I have done is to divide it in four sections ok the first thing is we have to create a verification activity I already created this activity and you can get this code in the description then the first thing to verify the OTP is to create a function which named sent verification on users form so we are not going to write users form but we will create a function first one send verification code which will send the code to user screen the number provided then we will create a callback which will be verification state callback then after this callback we will simply verify the court it only has a one single line and then after the verification we will sign in our users it is very simple we just have to follow these four points and OTP will be ready so let's start coding okay first of all we are in the signup screen and when the user click on this go button what we want to do is we want to send this phone number inside our verification activity inside the signup for Java in the previous tutorials we were storing this data on the click of this go gotten into the firebase but today before storing this data we want to verify the form of the first so for that currently I am going to mend these two lines and what we are going to do inside this register view button we first want to call the verification screen so to do that simply right indent 10th is equal to new intent and inside we will write get application context or you can write the application name which is sign absorb this from this activity from this context to verify phone number dot class and then with this intent what we want to attach is intern dot put extra and inside this put extra you can see we first have to name our value and then we have to send the value itself so it can be anything so the name will be phone number and with that we are going to send the phone number which is here okay now start that activity and pass the Intendant okay what we are doing is we are basically sending this phone number and calling our next activity which is verify phone number so as I mean calling login screen here so let meant this code as well okay we want to receive this phone number inside our verify phone number activity so in here first of all we need to create hooks of these three things to use in our verify phone number Java to use it inside of a Java function so first of all we have a button which is verify BTN and then click on add interest then we have edit text which is phone number entered by the user so if you guys should avoid any confusion that's why I am writing these gods names these boys variables and then the last one is a progress bar simply make it progress bar and then we need to create the hooks very five button and then press ctrl alt then press ctrl D to duplicate them two times and change this to and the second one is verification code entered by the user and at last we have a progress bar with the underscore bar okay change it to form number entered by the user and then make it progress bar you created the hooks and now what we want to do is as I told you will first want to create our method which will send the verification code okay after the hooks we want to get that phone number we are sending from this sign up which is phone number and we want to get this inside our verification activity to get that inside the string phone number is equal to get intent dot get string extra and and inside we have to pass the phone number so this name should be same as you have provided in here which is phone number simply copy this and inside you can paste it for the verification okay okay we have a phone number which was entered by the user in signup so the first function is send verification code to use it for your understanding I am writing these names and inside send verification on this form number and you pass in the phone number here click on this red bulb and create a methods and verification to user or sorry soldiers it is not send verification it is send a verification code to change it here as well send verification code to user and inside we have a form so what we want to do is we want to send verification code to user and for that open browser and search for firebase okay inside click on this Doc's button here and inside this dog click on this get started for Android okay in here you can search for authentication and in Android for phone number okay inside this phone number you can search for this method which is form or provider simply copy this and we are going to paste this method inside our send verification code to user because this code will send the verification code to our users press alt enter we are missing something we first need to go to the tools and inside go to the firebase to make a connection with the firebase sorry about that go to the authentication because we already storing our data inside our firebase so we are not going to do something with real time database but we are going inside our authentication click on this email and password and you can see we need to add some dependencies here to use the authentication so click on add firebase authentication to your elf and accept the changes okay once verified come on the here click on this arrow and press alt enter and it will automatically incorporate all the things and again press alt enter okay we have two errors but before going further first you need to make sure that your firebase is connected and then you have to add the dependencies in the project then open firebase and go to the console and open the project we are currently working on it is both run and go to the authentication in here we first need to set the sign-in methods setup the sign-in methods or you can also click on the menu now you can see we have a phone number here but it is currently disabled so first of all we need to enable this click on this enable button ok click on the Save button just make sure that it should be enabled once it's done come back to the code and we have pasted a form or provider basically this function which is saying verify phone number is used to verify the phone or to send the code to our number which is provided in here so as you know we have a phone number here which is four-member soul so this phone number only works with the country code so first of all let me add country code as say Pakistan came to code don't know about the others and I am going to create the another video which will be covering the whole country in the next coming videos so in here we have passed the phone number which is this one copy and replace it with here now what I have done is I have added the country code and then we have shredded the phone number which will be entered by the user then this will be a time how much time it will take to send that code and then time will be in seconds this is a time unit it will be in seconds and then we have to change this to a thread so instead of this what we want to do is we want to create a thread which is task executors dot main thread so main thread and then this will basically create a thread in it to send the verification phone number and then we have a last thing which is callbacks basically this callbacks means that we need to create a second function and the second function will be private form auth provider dot on verification state change callbacks and then we have to create this variable which is saying M callbacks if this should be same this M callbacks and this one written over here okay then we need to create the instance is equal to new and simply press control space and then we have a form or provider click on this and it will automatically override two functions and then at the bottom we have an error because we are missing a semicolon okay we have two functions in here the first one is saying on verification completed in the second one is on verification field so in on verification failed we just want to show a toast message and this toast will be saying the error ye dot get message okay so in on verification failed we are just writing the message which will be error basically and then in on verification completed this form auth provider function will only be called when the process is getting very very fine automatically the first function will be called at by any means because we are writing it inside our on create method so when the application launch or when this activity appears so it will automatically launch this function or call this function with a send verification code to user and this function is going to call this one so the function which is on verification completed is basically the function will execute automatically for the simpleness let's say code has been sent if there's a problem call this function and if everything is working fine simply verify the user so for that I am going to write string code is equal to which is form auth credential dot get SMS code so this is the core basically interred whether user which user I am getting this code basically entered by the user inside the text field and then I am going to write if code not equal to null because it should not be now and then what we want to perform is we want to show our progress bar dot set visible or visibility and visibility will be view dot visible and then we are going to call another function which will be our third function verify code and that's it in here we want to pass the code and in here we need to pass this quote we just get from the user okay this process is basically working in an automatic way but what if the user entered the phone number which is not in the same mobile but it is using some other device so for that we have to override not inside this verification completed but in here right click and go to the generate and override methods and here we have to override on old sent every click and it is here so what if the code has been sent but it is not on the same device if it is on the same device then this will be executed and this function will be called and everything we work fine but this one will be executed all the times on code sent and we just want to get this code to verify manually if in case that the device is changed so for that we are going to simply write a string which will store the ID or the verification code you can say verification code by system let's say let's you can change this variable these other variables is equal to s so so this s will contain the code which is verification code by system so in case if the code is sent this will be executed all the times in the code is sent we just want to store it inside our global variable so if the code has been sent and the automatic will process and the automatic verification completed or it's working then it will simply call this verify code and we are going to write this shortly so first of all create this strain at the dog which would be global which is this one cuz want to store it and okay the Harrow is gone okay that's it for the course and I hope you guys are you making you guys clear if you are confused anywhere you can tell me in the comments okay we have written a code for this for the arrow and now we want to create the code which is called verified code so you the function which is called vaguely code and close this on verification and that third function is private void very easy way the code and okay inside this verify code we are going to write string verification code inside this verification code as I told you at the start we just have to write a single line which will simply create a credential so for that write on earth contention and it credential is equal to form of provider this time dot get credential and in here we will pass two things the first one is the code we initialize globally which will be the system code so verification code by system and then the code which is this one verification code let me change the name for better understanding code by user okay now we have the code which is a system verification code and then the code by user then we have created a credential so that's it for this third function that we have to call our last function which is you can change the names but I am going to write sign in by credentials maybe and we want to pass the credential here and that's it okay our third function has been completed now we are going to last function which is sign in the credentials for this we first need to create the firebase or firebase or you can name it firewall which will be equal to 5 this auth dot get instance here we have any slide about firebase auth and now what we want to do is now let's call fire this auth dot sign in user with credentials and in here we want to pass this credential we just get it and then again press dot and add on complete listener so for that write the name of the class inside it way if i form dot this and then new on complete listener so it will automatically get it you can simply write new space alt + space then you can automatically click on that it will generate this for function and again put semicolon at the end ok this is the last function and in this case everything is completed so there is two things that can happen the first one is if the task is successful - dog is successful then then in here you can do or you can perform anything you want to store that user you want to update that user you want to login you now you can do anything and then in the else you can simply show the error toast dog make text in front of this so now the error will be so task dot get exception dot get message okay if if the action has been completed and then we have a two case or the two scenarios the first one is on a successful then the error one so our code is almost ready so just write something okay for this tutorial I am just calling if it is successful then call our next activity simply write intent intent is equal to new intent and in the new intent I'm going to pass get application context dot from file activity is the user profile dot class and that's it start that activity Android intent so normally this is a case that we write start activity and then pass the intent but for now this way user can easily get back to the verification phone activity but we don't want to allow the user to press the back button and again get back to this verification phone activity so for that we will be writing the flags here which will be intent dot set flags and inside it we need to pass two things the first one is we need to start a new task intent dot new task and the second one is in ten dot we want to clear the whole list of the tasks already yes the first one flag activity clear task so the this will clear the whole tasks already in the queue and then it will simply call this next activity ok that's it for the code let's run our application and open the firebase and you can see we currently have no users for this project and we are in the sign-in screen let's go to the sign up screen if you don't know how to create these designs these are material designs and we are currently working on the fire base as well you can get all these tutorials down below in the description okay let's run the application then what the phone number and the password will be something okay click on this Co button and yes our progress bar is working and wait for the court to arrive I hope it will be working and yes you can see our code has been arrived which is nine one five one zero three and it is six characters and we have arrived in our profile section so okay in here let's refresh and you can see our code has been arrived and as I told you there's a plus 9/2 which is the country code and it should be attached without any spaces okay now in here so we were sending the verification code to the user then checking the callbacks these were three callbacks and then we are verifying the code creating the credential and then signing in the user so what if this is basically an natural flow the automatic flows what if the user send the verification code to some other device and want to log in on some other device so for that we don't need to write any other function but we will see peak all that on click function the function will be first of all create the conflict method on the button verify button dot set onclicklistener new view dot set onclicklistener okay inside this on click what we want to do is we first want to get the code string code is equal to form number entered by the user dot get text the two strain okay first of all get the code and again right if this code dot is empty or court dot then is less than 6 which is phone number entered by the user this is the name of the field dot set the error and error could be wrong for TP and then again right for number two by the user dot request focus and simply return it form this function so if this is a problem if this is the case then return it else as we have created in the automatic version when the scroll bar starting scrolling so this is what we want to do we don't need else here we will simply write the progress bar dot set visibility and also make it visible view dot visible and simply call the verify code here and pass the code okay let me quickly oh we have missing something we are not on spot of visibility we need to first make this progress bar go at the top okay in here first of all what we are doing is okay we have created the onclick method and calling this verify code so as you can see above this on click listener we have we are calling this function which is sending the code so the first thing is sure that we need to send this verification code on this form number which is the first function this function will execute and send the code on this number and take this time then it will call this callbacks function which is the second function and in here in the second function open this and you can see we have a code sent so at any cost this function is calling by all means and then it is calling this function by all means so inside this second function which is on verification state change corvex own code sent will be executed at any cost but on verification will be called verification field will be called only when the verification has been filled and verification completed will be called when if everything works automatically so this function means everything works automatically and inside this on-click i am saying that if user need to enter the code manually so you can see i am showing the progress bar here and calling the verify code and again in the automatic way I am user don't need to click anywhere and I am showing the progress bar and user have to wait by the and calling the verify court method okay so in the third function the verify code will simply create a credential by getting the verification code by the system and the core by the user and create a credential and then we pass this credential to our method we have created sign in the user by credential using the credential method by credential method means there is a built-in function of firebase which is saying dot sign-in with credentials so we pass it over here it will simply check that the user the phone number entered by the code entered by the user and the system code has been matched if it is true then it will come inside as dog is successful function and Agricole what we have written here otherwise it will show the error so let's run it again and let's send the code on some other device on some other number and wait for the code to arrive and internet manually so run the application okay go to the signup screen and in here again write some details and the mail then this time the phone number will be different and password is something and click on this cooperton so strong password click on this KO button and you can see currently a loader is not loading and wait for the code to arrive ok code has been arrived and still you can see the load is not floating but let's write the code 6 and click on this verify button you can see it is very fine and once it's verified it will call the profile screen or the action you have written inside it ok that's it for today's tutorial if you learn something new please like the video and if you are new to the channel don't forget to subscribe it and hit the bell icon for more video updates you can check our website which is the MU Sikandar calm link is in the description and you can get all type of quotes you can also subscribe on our website to get the codes inside your mailbox so thank you for watching take care of us
Info
Channel: Coding With Tea
Views: 80,901
Rating: undefined out of 5
Keywords: firebase phone authentication, firebase phone authentication android, firebase mobile authentication, firebase authentication using phone number android, phone authentication firebase android, phone auth firebase android, firebase sms authentication android, firebase otp authentication, firebase phone auth, phone authentication firebase, phone authentication, firebase authentication, android otp, firebase otp verification android, otp verification in android studio
Id: 4G9p9BC9eYA
Channel Id: undefined
Length: 30min 22sec (1822 seconds)
Published: Mon Feb 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.