Wednesday , January 12 2022

Firebase Phone Authentication Android Tutorial – Android Coding by DMTechnolab

The package The trap.Simplified coding.Agnibesophenothentication;

Import Android.Content.Intent;

Import Android..Bundle;

Import Android.Support.Annotation.No no;

Import Android.Support.The design.Widget.the cafeteria;

Import Android.Support.v7.App.AppCompatActivity;

Import Android.Opinion.Opinion;

Import Android.Widget.Change in writing;

Import Android.Widget.Toast;

Import With.Google.Android.Village.Work.OnCompleteListener;

Import With.Google.Android.Village.Work.Work;

Import With.Google.Android.Village.Work.Job executor;

Import With.Google.Fire.FirebaseException;

Import With.Google.Fire.Justification.Authentic;

Import With.Google.Fire.Justification.FirebaseAuth;

Import With.Google.Fire.Justification.FirebaseAuthInvalidCredentialsException;

Import With.Google.Fire.Justification.Phoneartcredient;

Import With.Google.Fire.Justification.Phone mouthprovider;

Import Java.Useful.Concurrent.Time unit;

public class VerifyPhoneActivity Is spread AppCompatActivity {{

// these are essential items

// This is the verification ID that will be sent to the user

Private String mVerificationId;

// code to input edittext

Private Change in writing editTextCode;

// Firebase object object

Private FirebaseAuth mAuth;

@ Override

Protected Zero Go ahead()Bundle saveInstanceState) {{

Super.Go ahead()saveInstanceState);

setContentView()R.Layout.activity_verify_phone);

// initializing objects

mAuth = = FirebaseAuth.get Instance());

editTextCode = = findViewById()R.Id.editTextCode);

// get mobile number from previous activity

// and sending the verification code to the number

Intent is intended = = Get());

String mobile = = is intended.getStringExtra()“mobile”);

sendVerificationCode()mobile);

// If automatic SMS detection does not work, the user can also manually enter the code

// then add a click listener to the button

findViewById()R.Id.buttonSignIn).setOnClickListener()New Opinion.OnClickListener()) {{

@ Override

public Zero on click()Opinion v) {{

String Code = = editTextCode.Gettext()).Wire()).Trim());

If ()Code.Is Empty()) || Code.Length()) < 4) {{

editTextCode.on the set()“Enter valid code”);

editTextCode.request());

The return;

}

// code entered manually

VerVerificationCode()Code);

}

});

}

// method is sending verification code

// Country ID is truncated

// You can also take country id as user input

Private Zero sendVerificationCode()String mobile) {{

PhoneAuthProvider.get Instance()).VerPhoneNumber()

“+39” + mobile,

40,

Time unit.SECONDS,

Job executor.MAIN_THREAD,

mCallbacks);

}

// callback to detect verification status

Private Phone mouthprovider.OnVerificationStateChangedCallbacks mCallbacks = = New Phoneathprovider.OnVerificationStateChangedCallbacks()) {{

@ Override

public Zero onVerificationCompleted()Phoneartcredient phone) {{

// Receiving the code sent by SMS

String Code = = phone.getSmsCode());

// Sometimes the code is not detected automatically

// In this case the code will be null

// then the user has to enter the code manually

If ()Code ! = Invalid) {{

editTextCode.Settext()Code);

// Verifying the code

VerVerificationCode()Code);

}

}

@ Override

public Zero onVerificationFailed()FirebaseException I) {{

Toast.Makeup()VerifyPhoneActivity.this, I.getMessage()), Toast.LENGTH_LONG).Display());

}

@ Override

public Zero onCodeSent()String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {{

Super.onCodeSent()s, forceResendingToken);

// storing the verification ID sent to the user

mVerificationId = = s;

}

};

Private Zero VerVerificationCode()String Code) {{

// create credentials

Phoneartcredient Credit = = Phone mouthprovider.getCredential()mVerificationId, Code);

// user signature

Sign inphonephone feature()Authentic);

}

Private Zero Sign inphonephone feature()Phoneartcredient Credit) {{

mAuth.Sign in financial()Credit)

.addOnCompleteListener()VerifyPhoneActivity.this, New OnCompleteListener()) {{

@ Override

public Zero That which has been completed()@Nonal Work Work) {{

If ()Work.Is successful())) {{

// validation successful we will start profile activity

Intent is intended = = New Intent()VerifyPhoneActivity.this, Profile activity.class);

is intended.Setflag()Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

Beginning()is intended);

} other {{

// Verification failed .. display an error message

String message = = “Something’s wrong, we’ll fix it soon …”;

If ()Work.exception()) instance of FirebaseAuthInvalidCredentialsException) {{

message = = “Invalid code entered …”;

}

the cafeteria the cafeteria = = the cafeteria.to make()findViewById()R.Id.Parent), message, the cafeteria.LENGTH_LONG);

the cafeteria.Setation()“Dismiss”, New Opinion.OnClickListener()) {{

@ Override

public Zero on click()Opinion v) {{

}

});

the cafeteria.Display());

}

}

});

}

}


Source link

About dmtechnolab

Check Also

Simplified Coding

Android Espresso Tutorial – Testing Fragment in Isolation – Android Coding by DMTechnolab

Welcome to another tutorial in the Android test series. And this post is called Android …

Leave a Reply

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