In others Firebase version with removeUser we can delete an user only using email and password. With the new Firebase version it seems you can only delete and user if you have connected with that one... But the problem comes when I am connected with the admin user and I tried to delete the other user. This is the code.
final FirebaseUser user = mAuth.getCurrentUser();
AuthCredential credential = EmailAuthProvider
.getCredential(mail, postSnapshot.getValue(User.class).getPwdUser());
user.reauthenticate(credential)
.addOnCompleteListener(new OnCompleteListener < Void > () {
#Override
public void onComplete(#NonNull Task < Void > task) {
user.delete().addOnCompleteListener(new OnCompleteListener < Void > () {
#Override
public void onComplete(#NonNull Task < Void > taskDeleted) {
if (taskDeleted.isSuccessful()) {
Toast.makeText(getApplicationContext(),
"Deleted user!", Toast.LENGTH_LONG).show();
}
}
});
}
});
But I can't get delete method because on reauthenticate it throws an error with "The supplied credentials do not correspond to the previously signed in user." Anyone knows how I can reauthenticate from another user?
The Firebase SDK for Android can only delete the currently logged in user. So if you know the user's email+password, you'll have to sign in as that user to delete the account.
For admin functionality you should use the Firebase Admin SDK, which you should run on a trusted back-end server. Authentication functionality currently is only available in the Firebase Admin SDK for Node.
Finally the code should be something like this
mAuth.signOut();
mAuth.signInWithEmailAndPassword(email,password)
.addOnCompleteListener(UserList.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
mAuth= FirebaseAuth.getInstance(myFirebaseRef.getDatabase().getApp());
try{
mAuth.signInWithEmailAndPassword(getsPreferences().getString("mailUser",""), getsPreferences().getString("pwd",""))
.addOnCompleteListener(UserList.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
}});
}catch(Exception e){
}
}
}});
Related
I've been working on an Admin Control panel on Android. inside which admin can add and disable the users.
Suppose the admin uid is I4YnygVk2eaCLEJbCiCLiWlo13as
mAuth.createUserWithEmailAndPassword(emailid, password)
.addOnCompleteListener(UserManagement.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful()+"uid"+mAuth.getCurrentUser().getUid());
//here the Uid is changed to the new registered user i.e RlhiQxMibWYA1NaqlN9JdFZ8ocK2.
AuthCredential credential = EmailAuthProvider
.getCredential("a#a.com", "123456");
firebaseUser.reauthenticate(credential)
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
//here what i need is getUid() should print the admin's uid(I4YnygVk2eaCLEJbCiCLiWlo13as), and not the newly created uid.
Log.d(TAG, "User re-authenticated."+firebaseUser.getUid()) database.getReference("users/"+mAuth.getCurrentUser().getUid()).child("active").setValue(true);
}
});
}
});
When your user creation is successful call this method
FirebaseAuth.getInstance().signOut();
this would signout the newly created user and then you can signin with admin credentials.
Having created a new account, I automatically enter it.
How do I prevent my account from being created?
firebaseAuth.createUserWithEmailAndPassword(em, pass).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(getApplicationContext(), "Пользователь добавлен", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
} else {
Toast.makeText(getApplicationContext(), "Пользователь не добавлен", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}
});
May be need workes whis admin sdk?
May be who worked with admin sdk firebase android?
Are there any examples or lessons on working with admin sdk?
For I do not understand how to work with this.
The way I understand Firebase to work is that if the task is successful then Firebase actually actually signs in the user.
firebaseAuth.createUserWithEmailAndPassword(em, pass).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseAuth.getInstance().getCurrentUser();
//Ther will be a current user at this point. This is the way Firebase just works.
progressDialog.dismiss();
} else {
Toast.makeText(getApplicationContext(), "Пользователь не добавлен", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}
});
So you can actually get the user after the task is successful because the user is automatically set if the task is successful. If you would not like there to be any user when the task is successful. You can call
FirebaseAuth.getInstance().signOut();
This will sign out the user and when you call
Firebase.getInstance().getCurrentUser()
This will return nothing, that way there will be no user in your system.
firebaseAuth.createUserWithEmailAndPassword(em, pass).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseAuth.getInstance().getCurrentUser();
//Ther will be a current user at this point. This is the way Firebase just works.
FirebaseAuth.getInstance().signOut();// sign out the user
Firebase.geInstance().getCurrentUser(); // this will now be null
progressDialog.dismiss();
} else {
Toast.makeText(getApplicationContext(), "Пользователь не добавлен", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}
});
I signed up with Google. Then I logged out and tried logging in using the same email address as Facebook. As I expected, the user has already registered the error. When I received the error, I gave it the credential to linkWithCredantials method that I got from Facebook. Of course I also got getCurrentUser null error here, because any user logged in. I don't get it well, when will I link auth providers? And my case I want to link auth providers with out login. Is it possible?
P.S.: Sorry for my English.
Here is my example :
private void handleFacebookAccessToken(final AccessToken token) {
Log.d(TAG_FACEBOOK, "handleFacebookAccessToken:" + token);
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
final AuthCredential linkCredential = credential;
mAuth.signInWithCredential(credential)
.addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Log.d(TAG_FACEBOOK, "signInWithCredential:onComplete:" + task.isSuccessful());
if (task.isSuccessful()) {
replaceFragment(new ProfileFragment());
} else {
toast("Facebook authentication failed.");
}
}
}).addOnFailureListener(getActivity(), new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
if (e.getMessage().contains("account already exists")) {
//**here**
mAuth.getCurrentUser().linkWithCredential(linkCredential)
.addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Log.d(TAG_FACEBOOK, "linkWithCredential:onComplete:" + task.isSuccessful());
if (!task.isSuccessful()) {
}
}
});
}
}
});
}
To link two accounts, the user must sign in to both accounts. So they're already signed in as one of the accounts, which is then Firebase Authentication's currently signed in user. Then to link a different account to this, you provide the credentials for that account.
I have implemented Firebase authentication with email and password, here is my code
mFirebaseAuth.createUserWithEmailAndPassword(edtEmail.getText().toString(), edtPassword.getText().toString()).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser user = task.getResult().getUser();
Toast.makeText(RegisterActivity.this, "User registered successfully " + user.getEmail(), Toast.LENGTH_SHORT).show();
user.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
finish();
Log.e("RegisterActivity", "onComplete: " + task.getResult().toString());
} else
Log.e("RegisterActivity", "onComplete: " + task.getException().toString());
}
});
} else {
Toast.makeText(RegisterActivity.this, task.getException().toString(), Toast.LENGTH_SHORT).show();
}
}
});
After getting registered successfully i am sending confirmation mail to user, but getting this error.
com.google.firebase.FirebaseException: An internal error has occurred. [ USER_NOT_FOUND ]
I have checked it in firebase also, my user is registered and it is available there, but somehow i am not able to send confirmation mail.
I copied and ran your code with Firebase 9.6.1. When I passed an email address to createUserWithEmailAndPassword() that had never been used before, sendEmailVerification() completed successfully. I observed the failure you reported when I used an email address for a user that I had previously created and then deleted at the Firebase console. Are you seeing the failure when you use addresses you have used before and then deleted?
Note that createUserWithEmailAndPassword() not only creates the user, but also, if successful, signs the user in. When the creation and sign-in occurs when there is an existing signed-in user, there appears to be a Firebase bug related to signing out and clearing the cache for the previous user.
I was able to make your code work for a previously signed-in and later deleted user by calling signOut() before createUserWithEmailAndPassword().
You should use AuthStateListener() to see when a user is signed in that will ensure that user has successfully created and logged in then you can verify your user.
private FirebaseAuth.AuthStateListener mAuthListener;
mAuthListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
VerifyYourUserWithEmail();
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
// ...
}
};
#Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
#Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
Can I delete not authenticated user's account? The docs offers such a way:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
user.delete()
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "User account deleted.");
}
}
});
Is this the only way to delete account?