Reputation: 91
i am creating application where user have to verify his phone no and has to signup with his email and password and other filed like dob and all. first user will verify phone no so i have used phone no authentication and once done user will navigate to email and password signup. so here user will signup with email authentication. its creating two userid. so i want under one userid for both authentication.
PhoneAuthentication.java
private void signInWithCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(GetOTPActivity.this, "Mobile no authentication is successfully !!!", Toast.LENGTH_SHORT).show();
Intent intent;
intent = new Intent(GetOTPActivity.this, Register.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
} else {
Toast.makeText(GetOTPActivity.this, task.getException().getMessage(),
Toast.LENGTH_LONG).show();
}
}
});
}
Email authentication.java
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(Register.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Toast.makeText(Register.this, "Signup Successfully", Toast.LENGTH_SHORT).show();
if (!task.isSuccessful()) {
spotsDialog.dismiss();
Toast.makeText(Register.this, "Signup failed." + task.getException(),
Toast.LENGTH_SHORT).show();
} else {
spotsDialog.dismiss();
userID = mAuth.getCurrentUser().getUid();
User user = new User(edtName.getText().toString(), edtPhone.getText().toString(),
edtEmail.getText().toString(), edtPassword.getText().toString(), edtConfirmPassword.getText().toString(), false);
user_tbl.child(userID).setValue(user);
startActivity(new Intent(Register.this, Login.class));
}
}
});
}
});
Upvotes: 2
Views: 389
Reputation: 1420
All you have to use is linkWithCredential() where you have to pass the credential now this credential can be from any auth like facebook ,gmail.
Try this code :
mAuth.getCurrentUser().linkWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(TAG, "linkWithCredential:success");
FirebaseUser user = task.getResult().getUser();
updateUI(user);
} else {
Log.w(TAG, "linkWithCredential:failure", task.getException());
Toast.makeText(AnonymousAuthActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
updateUI(null);
}
// ...
}
});
Upvotes: 1