我知道如何将Firebase与电话身份验证或电子邮件一起使用,但我必须同时使用-应用程序中的电话号码和电子邮件号码才能存储在数据库中。 可能吗 ? 如果是这样,请在下面提供帮助。 我在Firebase上使用电话otp进行身份验证。 如何通过其他字段将身份验证表中的电话号码存储到数据库表中。
答案 0 :(得分:0)
您可以只为用户放置两个选项。您可以放一个按钮以使用Google ID登录,另一个放一个按钮以登录其电话号码。
如果您不知道该怎么做,只需添加相关按钮,然后在java
部分中使用类似以下内容的方法即可:
这是获取和验证OTP,使用Google ID和电话号码登录的部分。
还有更多功能,例如使用应用程序的Google登录按钮signIn()
中的onClickListener()
方法,但我认为您可以自己完成此操作。
private void signIn(){
Intent sIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(sIntent,RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(requestCode == RC_SIGN_IN){
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if(result.isSuccess()){
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
}
else{
Toast.makeText(MainActivity.this,"Auth went wrong",Toast.LENGTH_SHORT).show();
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount account){
AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(),null);
mAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Log.d("Tag","SignInWithCredential: success");
FirebaseUser fUser = mAuth.getCurrentUser();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
assert fUser != null;
// do what you want on successful signIN, like storing the email using ref
}
else{
Log.w("TAG","SignInWithCredential: failure", task.getException());
Toast.makeText(MainActivity.this,"Authentication failed", Toast.LENGTH_SHORT).show();
}
}
});
}
private void GetOTP(){
String pn = phone.getText().toString();
if(pn.isEmpty()){
Toast.makeText(MainActivity.this,"Phone number is required", Toast.LENGTH_SHORT).show();
}
else if(pn.length()<10){
Toast.makeText(MainActivity.this,"Valid Phone number is required", Toast.LENGTH_SHORT).show();
}
else
PhoneAuthProvider.getInstance().verifyPhoneNumber(pn,60, TimeUnit.SECONDS,this,mCallBacks);
}
PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallBacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
}
@Override
public void onVerificationFailed(FirebaseException e) {
}
@Override
public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
otp = s;
}
};
private void verifyCode(){
String code = cd.getText().toString();
String pH = phone.getText().toString();
if(code.equals("") && pH.equals(""))
Toast.makeText(MainActivity.this,"Nothing to validate", Toast.LENGTH_SHORT).show();
else {
try {
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(otp, code);
signInWithPhoneAuthCredential(credential);
}
catch (Exception e) {
Log.i("exception",e.toString());
Toast.makeText(MainActivity.this,"Invalid credentials",Toast.LENGTH_LONG).show();
}
}
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential){
mAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
startActivity(new Intent(MainActivity.this,Main2Activity.class));
}
else if(task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
Toast.makeText(MainActivity.this, "OTP is incorrect", Toast.LENGTH_SHORT).show();
}
}
});
}
获取电子邮件ID或电话号码后,您可以将其设置为Firebase数据库中用户的identifier
。
我的意思是,您可以将该用户的所有其他详细信息保存在您获得的电话号码/电子邮件下。
代码看起来像这样:
//email is the email of the user and phone is the phone Number
if(email==null){
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref.child("users").child(phone).child("username").setValue(uName);
// you can set other details in the same way
}
else if(phone==null){
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref.child("users").child(email).child("username").setValue(uName);
// you can set other details in the same way
}
// uName is the username you want to store for the user