This question already has an answer here:
How to delete a registered user by using firebase auth in android studio?
(1 answer)
Closed 4 years ago.
I have created one application, in that I am managing three levels of users like "Admin", "Sub Admin" and "Customers"
Now I am adding Functionality of "Delete Sub Admin" that I want to remove from Authentication of Firebase.
I found some answers but that is for currentUser only.
I show deleteUser method in docs but I think its removed or deprecated.
Any other solution replaced for this method? Any clue?
Your help would be appreciated, Thanks.
You can use this code to delete an authenticated user:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
AuthCredential credential = EmailAuthProvider.getCredential("email", "password");
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> task) {
if (task.isSuccessful()) {
Toast.makeText(MainActivity.this, "User deleted", Toast.LENGTH_SHORT).show();
}
}
});
}
});
The above code first checks if a user is authenticated or not by re-authenticating, and it deletes the user if the task is successful.
Related
I have used the following code after the button clicked event :
mCurrentUser = FirebaseAuth.getInstance.getCurrentUser();
mCurrentUser.delete.addOnCompleteListner(............)
and also used if and else statement for appropriate actions for the task to be done.
But nothing's happening in the application on that click event, also not deleting the firebase user account. Help me regarding this.
thank you.
try with this answer, I had tried with this
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.");
}
}
});
I have used Firebase serval times by now and I am aware to how use it correctly.
I believe I have been stumble on a bug and would love to hear a solution in case you have one.
So - I used Firebase Google auth and it worked ok, then I decided to use the email + password method.
Now this is the weird part - I create a user, I get successful result but it doesn't save the user - cant see it in the Autherecation panel + cant sign in with the user after I sign out.
mLoginContainer.setVisibility(View.GONE);
mLoginProgress.setVisibility(View.VISIBLE);
mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful())
Toast.makeText(LoginActivity.this,"",Toast.LENGTH_SHORT).show();
else
Toast.makeText(LoginActivity.this,"",Toast.LENGTH_SHORT).show();
mLoginContainer.setVisibility(View.VISIBLE);
mLoginProgress.setVisibility(View.GONE);
}
});
You need to enable E-mail/Password on firebase console.
Click here for Firebase Auth Doc
How to enable E-mail/password sign-in:
In the Firebase console, open the Auth section.
On the Sign in method tab, enable the Email/password sign-in method and click Save.
hope it helps
Refer this
Hope you have added compile 'com.google.firebase:firebase-auth:9.2.1'in gradle dependencies
Enabling Email/Password Authentication
Go to your firebase panel.
On the left side menu you will see Auth, click on it.
Now click on Set Up Sign In Method.
Now click on Email/Password, enable it and press save.
For SignUp
In your Registration activity
//defining firebaseauth object
private FirebaseAuth firebaseAuth;
onCreate
//initializing firebase auth object
firebaseAuth = FirebaseAuth.getInstance();
//after the successful validation of username and password[call your registration method with following)
//creating a new user
firebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
//checking if success
if(task.isSuccessful()){
//display some message here
Toast.makeText(this,"Successfully registered",Toast.LENGTH_LONG).show();
}else{
//display some message here
Toast.makeText(this,"Registration Error",Toast.LENGTH_LONG).show();
}
progressDialog.dismiss();
}
});
Click on Signup button in your design and if you got the success message. Check your firebase console.
For SignIn
mAuth.signInWithEmailAndPassword(strUsrL,strPassL)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
progressBar.setVisibility(View.GONE);
if(task.isSuccessful()){
Toast.makeText(this, "Successfully Login", Toast.LENGTH_SHORT).show();
}
}})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
Log.e("EXCEPTION",e.getLocalizedMessage().toString());
}
});
Reinstall application in emulator and try to provide an emulator with play store installation
I want to check when a user attempts to signup with createUserWithEmailAndPassword() in Firebase user Authentication method, this user is already registered with my app.
To detect whether a user with that email address already exists, you can detect when the call to createUserWithEmailAndPassword () fails with auth/email-already-in-use. I see that #Srinivasan just posted an answer for this.
Alternatively, you can detect that an email address is already used by calling fetchSignInMethodsForEmail().
The usual flow for this is that you first ask the user to enter their email address, then call fetchSignInMethodsForEmail, and then move them to a screen that either asks for the rest of their registration details (if they're new), or show them the provider(s) with which they're signed up already.
When the user trying to create an user with same email address, the task response will be "Response: The email address is already in use by another account."
mFirebaseAuth.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
//User registered successfully
}else{
Log.i("Response","Failed to create user:"+task.getException().getMessage());
}
}
});
First of all, you need to make sure you have that restriction enabled in Firebase console (Account and email address settings). Take a look at #Srinivasan's answer.
Then, do this in your java code:
firebaseAuthenticator.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
if (task.getException() instanceof FirebaseAuthUserCollisionException) {
Toast.makeText(SignUpActivity.this, "User with this email already exist.", Toast.LENGTH_SHORT).show();
}
} else {
sendVerificationEmail();
startActivity(new Intent(SignUpActivity.this, DetailsCaptureActivity.class));
}
// ...
}
});
This is where the trick happens:
if (task.getException() instanceof FirebaseAuthUserCollisionException) {
Toast.makeText(SignUpActivity.this,
"User with this email already exist.", Toast.LENGTH_SHORT).show();
Several exceptions can be thrown when registering a user with email and password, but the one we are interested in is the FirebaseAuthUserCollisionException. As the name implies, this exception is thrown if the email already exists. If the exception thrown is an instance of this class, let the user know.
As a practice of #Frank's answer here is the code of using fetchProvidersForEmail()
private boolean checkAccountEmailExistInFirebase(String email) {
FirebaseAuth mAuth = FirebaseAuth.getInstance();
final boolean[] b = new boolean[1];
mAuth.fetchProvidersForEmail(email).addOnCompleteListener(new OnCompleteListener<ProviderQueryResult>() {
#Override
public void onComplete(#NonNull Task<ProviderQueryResult> task) {
b[0] = !task.getResult().getProviders().isEmpty();
}
});
return b[0];
}
I was looking into this kind of condition where we can detect if user exists or not and perform registration and login. fetchProvidersForEmail is best option right now. I have found this tutorial. Hope it helps you too!
See : Manage Users
UserRecord userRecord = FirebaseAuth.getInstance().getUserByEmail(email);
System.out.println("Successfully fetched user data: " + userRecord.getEmail());
This method returns a UserRecord object for the user corresponding to the email provided.
If the provided email does not belong to an existing user or the user cannot be fetched for any other reason, the Admin SDK throws an error. For a full list of error codes, including descriptions and resolution steps, see Admin Authentication API Errors.
private ProgressDialog progressDialog;
progressDialog.setMessage("Registering, please Wait...");
progressDialog.show();
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
//checking if success
if (task.isSuccessful()) {
//Registration was successfull:
Toast.makeText(RegistrationActivity.this, "Successfully registered!", Toast.LENGTH_LONG).show();
} else {
//Registration failed:
//task.getException().getMessage() makes the magic
Toast.makeText(RegistrationActivity.this, "Registration failed! " + "\n" + task.getException().getMessage(), Toast.LENGTH_LONG).show();
}
progressDialog.dismiss();
}
});
Add below code to MainActivity.java file.When user attempt to register with the same email address a message "The email address is already used by another account" will pop up as a Toast
mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(!task.isSuccessful()){
Toast.makeText(MainActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
if(task.isSuccessful()){
Toast.makeText(MainActivity.this, "Sign up successfull", Toast.LENGTH_SHORT).show();
}
}
});
You do not have to do anything because the backend of Firebase will do the job.
Unless you are referring to reauthenticating of the app.
Take a scenario for an example, w
I see in the docs how to update your own email once you are logged in, like so:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
user.updateEmail("user#example.com")
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "User email address updated.");
}
}
});
What I want, though, is to be logged in as an user and be able to update the email of another user(The logged in user is an admin). With the approach above that's not possible, because you can get only the current user from FirebaseAuth.
Does anyone know how to achieve that?
There is currently no administrative API for Firebase Authentication. We're working on adding this to our server SDKs, but there is no definitive timeline for its availability yet.
I've created a Chat function on my android app, but I wanted my users to be able to create an account. However, I managed to let users create an account using Firebase. But the problem is that it allows you to create accounts with Facebook etc, I choose for email and password. This doesn't allow you to set an username, I believe so. Maybe there's a way to change the UID?
If anyone is able to help me out here, I'd be so thankful!
Again, I'm trying to let the user create an username too.
Have a look at the FirebaseUI implementation of its Email+Password registration flow:
firebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
final FirebaseUser firebaseUser = task.getResult().getUser();
Task<Void> updateTask = firebaseUser.updateProfile(
new UserProfileChangeRequest
.Builder()
.setDisplayName(name).build());
updateTask.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
mActivityHelper.dismissDialog();
if (task.isSuccessful()) {
startSaveCredentials(firebaseUser, password);
}
}
});
}
Alternatively, you might simple want to use FirebaseUI, which encapsulates this and many other auth flows.