我正在使用PhoneAuthCredential,它工作正常,但是在将他重定向到家庭活动之前,我需要检查用户是否提供了用户名和电子邮件之类的数据,如果找不到该数据,则必须将他重定向到注册活动,这样我就可以在登录之前获取这些数据
public class VerifyPhoneActivity extends AppCompatActivity {
private String verificationId;
private FirebaseAuth mAuth;
FirebaseAuth.AuthStateListener mAuthListener;
DatabaseReference users;
ProgressBar progressBar;
TextInputEditText editText;
AppCompatButton buttonSignIn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verify_phone);
mAuth = FirebaseAuth.getInstance();
progressBar = findViewById(R.id.progressbar);
editText = findViewById(R.id.editTextCode);
buttonSignIn = findViewById(R.id.buttonSignIn);
String phoneNumber = getIntent().getStringExtra("phoneNumber");
sendVerificationCode(phoneNumber);
// save phone number
SharedPreferences prefs = getApplicationContext().getSharedPreferences("USER_PREF",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("phoneNumber", phoneNumber);
editor.apply();
buttonSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String code = editText.getText().toString().trim();
if (code.isEmpty() || code.length() < 6) {
editText.setError("Enter code...");
editText.requestFocus();
return;
}
verifyCode(code);
}
});
}
private void verifyCode(String code) {
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
signInWithCredential(credential);
}
private void signInWithCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if(user!=null){
Intent intent = new Intent(VerifyPhoneActivity.this, DriverHome.class);
startActivity(intent);
finish();
}else {
Intent intent = new Intent(VerifyPhoneActivity.this, StartActivity.class);
startActivity(intent);
finish();
}
}else {
Toast.makeText(VerifyPhoneActivity.this, task.getException().getMessage(), Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
}
}
});
}
private void sendVerificationCode(String number) {
progressBar.setVisibility(View.VISIBLE);
PhoneAuthProvider.getInstance().verifyPhoneNumber(
number,
60,
TimeUnit.SECONDS,
TaskExecutors.MAIN_THREAD,
mCallBack
);
progressBar.setVisibility(View.GONE);
}
private PhoneAuthProvider.OnVerificationStateChangedCallbacks
mCallBack = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
verificationId = s;
}
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
String code = phoneAuthCredential.getSmsCode();
if (code != null) {
editText.setText(code);
verifyCode(code);
}
}
@Override
public void onVerificationFailed(FirebaseException e) {
Toast.makeText(VerifyPhoneActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
}
};
}
答案 0 :(得分:0)
问题还不清楚。您希望从哪个活动中获取此信息?
创建活动以获取用户的信息。
例如:-UserInformationActivity
在Java文件中,您可以创建一个Firebase数据库节点来存储此信息
数据库参考:
DatabaseReference userinfo=FirebaseDatabase.getInstance().getReference().child("User Info").child(FirebaseAuth.getInstance().getCureentUser().getUid());
使用Hashmap
将数据插入节点。
Hashmap hash = new Hashmap();
hash.put("Username", username);
hash.put("Email", email);
userinfo.updateChildren(hash);
在您的VerifyPhoneActivity
类中,当您成功验证用户身份后,请检查Firebase数据库中是否存在该引用的数据快照。像这样更新VerifyPhoneActivity
类中的代码:
final DatabaseReference us=FirebaseDatabase.getInstance().getReference().child("User Info").child(FirebaseAuth.getInstance().getCurrentUser.getUid())
并像这样更新您的signInwithCredential()方法:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if(user!=null){
us.addValueEventlistner(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
Intent intent = new Intent(VerifyPhoneActivity.this, DriverHome.class);
startActivity(intent);
finish(); }
else{
Intent gotoUserInfo=new Intent(VerifyPhoneActivity.this,UserInformationActivity.class);
startActivity(gotoUserInfo);
finish();
} }
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
else {
Intent intent = new Intent(VerifyPhoneActivity.this, StartActivity.class);
startActivity(intent);
finish();
}
希望这会有所帮助。谢谢!