Android firebase Phone verification authentication – OTP

Android firebase Phone verification authentication – OTP

Step 1 :

Go to firebase console . Go SIGN-IN METHOD and enable phone authentication option .

 

Step 2 :

Open your build.gradle (app) and paste this firebase ui gradle and sync . Check firebase version code . It may vary .

compile 'com.firebaseui:firebase-ui-auth:3.1.0'
compile 'com.google.android.gms:play-services-auth:11.4.2'

Step 3 :

 private static final int RC_SIGN_IN = 123; 

Step 4 :

Paste this on OnCreate method .

FirebaseAuth auth = FirebaseAuth.getInstance();
if (auth.getCurrentUser() != null) {
AuthUI.EXTRA_DEFAULT_NATIONAL_NUMBER.toString();
// SavedData.user = auth.getCurrentUser().toString();
startActivity(new Intent(context, MainActivity.class));
finish();
} else {
// not signed in
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(
Arrays.asList(
new AuthUI.IdpConfig.Builder(AuthUI.PHONE_VERIFICATION_PROVIDER).build()
))
.build(),
RC_SIGN_IN);
} 

Step – 4 :

implement this callback method

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // RC_SIGN_IN is the request code you passed into startActivityForResult(...) when starting the sign in flow.
    if (requestCode == RC_SIGN_IN) {
        IdpResponse response = IdpResponse.fromResultIntent(data);
        // Successfully signed in
        if (resultCode == ResultCodes.OK) {

            startActivity(new Intent(context, MainActivity.class));
            finish();
            return;
        } else {
            // Sign in failed
            if (response == null) {
                // User pressed back button
                Log.e("Login", "Login canceled by User");
                return;
            }
            if (response.getErrorCode() == ErrorCodes.NO_NETWORK) {
                Log.e("Login", "No Internet Connection");
                return;
            }
            if (response.getErrorCode() == ErrorCodes.UNKNOWN_ERROR) {
                Log.e("Login", "Unknown Error");
                return;
            }
        }
        Log.e("Login", "Unknown sign in response");
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *