BÀI 47 - XÁC THỰC ĐĂNG NHẬP BẰNG SỐ ĐIỆN THOẠI TRONG LẬP TRÌNH ANDROID
Thêm SDK Firebase vào ứng dụng của bạn. Khai báo chúng trong tệp Gradle mô-đun (cấp ứng dụng) của bạn (thường là app/build.gradle ).
Thêm thẻ SHA-256 nữa
dependencies {
implementation platform('com.google.firebase:firebase-bom:28.2.1')
implementation 'com.google.firebase:firebase-auth'
}
ProgressBar pgOTP;
String soDienThoai;
String codeOTP;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_otp);
TextView tvGuiLai = findViewById(R.id.textViewGuiLaiOTP);
TextView tvTiepTuc = findViewById(R.id.textViewHoanTatOTP);
EditText edtOTP1 = findViewById(R.id.editTextNumber4);
EditText edtOTP2 = findViewById(R.id.editTextNumber5);
EditText edtOTP3 = findViewById(R.id.editTextNumber6);
EditText edtOTP4 = findViewById(R.id.editTextNumber7);
EditText edtOTP5 = findViewById(R.id.editTextNumber8);
EditText edtOTP6 = findViewById(R.id.editTextNumber9);
pgOTP = findViewById(R.id.progressBarOTP);
soDienThoai = getIntent().getStringExtra("tenTaiKhoan");
SendOTPPhoneNumber(soDienThoai);
tvGuiLai.setOnClickListener(v -> SendOTPPhoneNumber(soDienThoai));
tvTiepTuc.setOnClickListener(v -> {
String OTP1 = edtOTP1.getText().toString();
String OTP2 = edtOTP2.getText().toString();
String OTP3 = edtOTP3.getText().toString();
String OTP4 = edtOTP4.getText().toString();
String OTP5 = edtOTP5.getText().toString();
String OTP6 = edtOTP6.getText().toString();
if (OTP1.isEmpty() || OTP2.isEmpty()
|| OTP3.isEmpty() || OTP4.isEmpty()
|| OTP5.isEmpty() || OTP6.isEmpty()) {
showToast(getString(R.string.OTP_empty));
} else {
String readOTP = OTP1+OTP2+OTP3+OTP4+OTP5+OTP6;
pgOTP.setVisibility(View.VISIBLE);
verifyCode(readOTP);
}
});
}
private void SendOTPPhoneNumber(String phone) {
StringBuilder sdt = new StringBuilder("+84");
for (int i=1; i<phone.length(); i++) {
sdt.append(phone.charAt(i));
}
PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallBacks
= new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
codeOTP = s;
}
@Override
public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
String code = phoneAuthCredential.getSmsCode();
if (code != null) {
pgOTP.setVisibility(View.INVISIBLE);
verifyCode(code);
}
}
@Override
public void onVerificationFailed(@NonNull FirebaseException e) {
if (e instanceof FirebaseAuthInvalidCredentialsException) {
showToast(getString(R.string.yeu_cau_khong_hop_le));
} else if (e instanceof FirebaseTooManyRequestsException) {
showToast(getString(R.string.vuot_qua_OTP));
}
}
};
PhoneAuthProvider.getInstance().verifyPhoneNumber(
sdt.toString(),
60,
TimeUnit.SECONDS,
this,
mCallBacks
);
}
private void verifyCode(String verifyCode) {
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codeOTP, verifyCode);
FirebaseAuth auth = FirebaseAuth.getInstance();
auth.signInWithCredential(credential)
.addOnCompleteListener(OTP_Activity.this, task -> {
if (task.isSuccessful()) {
Intent intent = new Intent(getApplicationContext(), CreateNewProjectActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
} else {
showToast(Objects.requireNonNull(task.getException()).getMessage());
}
});
}
private void showToast(String txt) {
Toast.makeText(this, txt, Toast.LENGTH_SHORT).show();
}
Kiểm tra đăng nhập
private FirebaseAuth mAuth;
mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
if (user != null) {
Đã đăng nhập
}