您可以为您的用户设置这两个选项。您可以放置一个按钮用于使用 Google ID 登录,另一个按钮用于输入他们的电话号码进行登录。
如果你不知道怎么做,你只需要添加相关按钮,在java部分,你可以使用这样的东西:
这是获取和验证 OTP,使用 Google ID 和电话号码登录的部分。
还有更多内容,例如在您应用的 Google 登录按钮的 onClickListener() 中使用 signIn() 方法,但我认为您可以自己执行此操作。
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();
}
}
});
}
获得 emailID 或电话号码后,您可以在 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