Skip to content

Instantly share code, notes, and snippets.

@jaisonfdo
Created March 20, 2020 04:53
Show Gist options
  • Save jaisonfdo/ba3494f1dc87cae1244d021faaab01d6 to your computer and use it in GitHub Desktop.
Save jaisonfdo/ba3494f1dc87cae1244d021faaab01d6 to your computer and use it in GitHub Desktop.
Firebase Auth - Password-less Phone - Demo
implementation 'com.google.android.gms:play-services-auth:17.0.0'
implementation 'com.google.firebase:firebase-auth:19.2.0'
<uses-permission android:name="android.permission.INTERNET"/>
// Initialize Firebase Auth
FirebaseAuth mAuth;
mAuth = FirebaseAuth.getInstance();
PhoneAuthProvider.getInstance().verifyPhoneNumber(
mobileNumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
activity, // Activity (for callback binding)
getVerificationCallback()); // OnVerificationStateChangedCallbacks
private OnVerificationStateChangedCallbacks getVerificationCallback() {
if (verificationCallback == null)
verificationCallback = new OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
Log.d(TAG, "onVerificationCompleted:" + phoneAuthCredential);
}
@Override
public void onVerificationFailed(@NonNull FirebaseException e) {
Log.w(TAG, "onVerificationFailed", e);
if (e instanceof FirebaseAuthInvalidCredentialsException) {
// Invalid request
if (e.getLocalizedMessage().contains("The format of the phone number provided is incorrect."))
Log.d(TAG, "onVerificationFailed: The format of the phone number provided is incorrect.")
else
Log.d(TAG, "onVerificationFailed: Invalid credential");
} else if (e instanceof FirebaseTooManyRequestsException) {
// The SMS quota for the project has been exceeded
Log.d(TAG, "onVerificationFailed: FirebaseTooManyRequestsException");
} else
Log.d(TAG, "onVerificationFailed");
}
@Override
public void onCodeSent(@NonNull String verificationId,
@NonNull ForceResendingToken token) {
// The SMS verification code has been sent to the provided phone number
Log.d(TAG, "onCodeSent:" + verificationId)
// Save verification ID and resending token so we can use them later
mVerificationId = verificationId;
resendingToken = token;
}
};
return verificationCallback;
}
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, code);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(activity, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");
if (task.getResult() != null) {
FirebaseUser user = task.getResult().getUser();
if (user != null && !TextUtils.isEmpty(user.getPhoneNumber()))
Log.d(TAG, "signInWithCredential: " + user.getPhoneNumber());
}
} else {
// Sign in failed, display a message and update the UI
Log.w(TAG, "signInWithCredential:failure", task.getException());
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
// Invalid request
Log.d(TAG, "signInWithCredential: Invalid");
} else if (task.getException() instanceof FirebaseTooManyRequestsException) {
// The SMS quota for the project has been exceeded
Log.d(TAG, "signInWithCredential: FirebaseTooManyRequestsException");
} else
Log.d(TAG, "signInWithCredential: Failed");
}
}
});
if (resendingToken != null) {
PhoneAuthProvider.getInstance().verifyPhoneNumber(
mobileNumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
activity, // Activity (for callback binding)
getVerificationCallback(), // OnVerificationStateChangedCallbacks
resendingToken); // Resending token received from onCodeSent callback
}
// For example use *es* for Spanish
mAuth.setLanguageCode("YOUR_LANGUAGE_CODE");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment