Setup Firebase
keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android
Coding Part
dependencies {
...
compile 'com.google.firebase:firebase-auth:11.0.0'
}
// Add to the bottom of the file
apply plugin: 'com.google.gms.google-services'
Add the dependency for Play Services to your project-level build.gradle file:dependencies {
...
classpath 'com.google.gms:google-services:3.1.0'
}
Send a verification code to the user's phone
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneNumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
this, // Activity (for callback binding)
mCallbacks); // OnVerificationStateChangedCallbacks
mCallbacks is used to know the verification status and the callback method is implemented as in the following
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
Log.d(TAG, "onVerificationCompleted:" + credential);
signInWithPhoneAuthCredential(credential);
}
@Override
public void onVerificationFailed(FirebaseException e) {
Log.w(TAG, "onVerificationFailed", e);
if (e instanceof FirebaseAuthInvalidCredentialsException) {
mPhoneNumberField.setError("Invalid phone number.");
} else if (e instanceof FirebaseTooManyRequestsException) {
Snackbar.make(findViewById(android.R.id.content), "Quota exceeded.",
Snackbar.LENGTH_SHORT).show();
}
}
@Override
public void onCodeSent(String verificationId,
PhoneAuthProvider.ForceResendingToken token) {
Log.d(TAG, "onCodeSent:" + verificationId);
mVerificationId = verificationId;
mResendToken = token;
}
};
onVerificationCompleted(PhoneAuthCredential)This method is called, when the user number verified successfully.
This method is called, when error occurred during verification.
onCodeSent(String verificationId, PhoneAuthProvider.ForceResendingToken)
This method is called when the verification code is send to user via SMS.
Create a PhoneAuthCredential object
The phone auth credits are created as in the following snippetPhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
// verificationId can be found in onCodeSent function in callback functionality
// code is retrieved from SMS send by Firebase
Then we can verify and store the Phone Authentication user details by using the following method.
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()) {
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = task.getResult().getUser();
startActivity(new Intent(PhoneAuthActivity.this, MainActivity.class));
finish();
} else {
Log.w(TAG, "signInWithCredential:failure", task.getException());
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
mVerificationField.setError("Invalid code.");
}
}
}
});
Sign Out
To sign out from your Application just use signout method in your Auth method.FirebaseAuth mAuth = FirebaseAuth.getInstance();
mAuth.signOut();
Download Code:
You can download the full source from the following Github link. If you Like this tutorial, Please star it in Github.Post your doubts and comments in the comments section.


Great article buddy, just I couldn't understand why to create, verification textview and verify button, as it autoverify the code. waiting for your reply
ReplyDeleteHi Buddy, Thanks for the response.
DeleteThe Create Button used to trigger verification for your entered phone number.
The Verification Button used to verify for your entered code against the entered phone number.
Your can implement the auto verification with Read SMS option.
Hi Mushtaq bro,
ReplyDeleteI am getting this error, when I added the code in my project.
NOTE :: I've already created the project on firebase and paste json file in app.
com.google.firebase.auth.FirebaseAuthInvalidCredentialsException: The custom token corresponds to a different audience. [ App ID does not match requested project. ]
By your comment I think you are trying to generate login to other device, if you are trying that then you couldn't do it as Firebase will verify the verificationid and token {you could check that from the logcat tagged onCodeSent:}
DeleteBro,
DeleteTry this, https://firebase.google.com/docs/reference/android/com/google/firebase/auth/Firebas
eAuthInvalidCredentialsException
https://stackoverflow.com/questions/37859582/how-to-catch-a-firebase-auth-specific-exceptions
thank u so much for this tutorial
ReplyDeleteWhen the user is signed out and redirected to a sign in screen where he enters the already verified phone number. Does the user get nother sms code or get instantly authenticated ?
ReplyDeletePID: 24926
ReplyDeletejava.lang.NullPointerException: null reference
at com.google.android.gms.common.internal.zzbo.zzu(Unknown Source)
at com.google.firebase.auth.PhoneAuthProvider.verifyPhoneNumber(Unknown Source)
at net.clamour.goups.chat.PhoneAuthActivity.startPhoneNumberVerification(PhoneAuthActivity.java:163)
at net.clamour.goups.chat.PhoneAuthActivity.access$000(PhoneAuthActivity.java:35)
at net.clamour.goups.chat.PhoneAuthActivity$1.run(PhoneAuthActivity.java:76)
By using Firebase some frequent functionalities like Login with Google,Facebook,Twitter are being very easy because it's almost readily available with firebase. Thank you!
ReplyDelete==============================
iot cloud services
Cell Phones utilized for significant stretches of time can cause growths in the cerebrum? mobile price
ReplyDeleteThis is helpful, nonetheless it can be crucial so that you can check out the following website: cell phone repair
ReplyDelete13EA73715D
ReplyDeleteCam Åžov
Ücretli Show
Görüntülü Seks