Firebase Auth: can't instantiate AuthCredential


Firebase Auth: can't instantiate AuthCredential



I am trying to make a Master User module, which user can delete other users. I read from other questions, that if you want to delete a user, You must logged in as that user. And then, I realize that you could sign in with credential information stored by firebase auth. (for references, I use google, facebook and email method).



I also store a User entity in my Firebase Database which correspond to Firebase Auth. Here is my User class :


public class User {
// variables.
private String appId;
private String email;
private String name;
private String description;
private String photoUrl;
private boolean isAdmin;
private boolean isSuspended;
private boolean isDeleted;
private AuthCredential credential;

// all those getters and setters go here.
}



Notice that I store a AuthCredential instance in my user, which I set for the first time when I sign up this user, ( or sign in with facebook and google).
Whenever I sign up / sign in, I got this error:


java.lang.RuntimeException: java.lang.InstantiationException: can't instantiate class com.google.firebase.auth.AuthCredential



BUT, I check my firebase database, and the credential is inserted.



1. FirebaseAuth using email and password.Firebase Auth using email and password.



2. FirebaseAuth using google providerFirebase Auth using google provider.



My question is, what is the right method to store my user credential in firebase database and recover it again?
Also, I don't know if what I'm doing is right, but my point is, I want to delete another user from some admin user. To do that, in my master user module, I'm gonna store my admin user credential, then I sign in as the user who will be deleted, delete myself, and the re-sign in to admin user by using stored admin user credential. Is there any other way to achieve this?



Thank You for your help.



EDIT



I got error whenever I do this :


User curUser = dataSnapshot.getValue(User.class);



... which is fine before I add AuthCredential instance in my user class.




4 Answers
4



AuthCredential is an abstract class and abstract classes cannot be instantiated, hence the error whenever you signup.
https://firebase.google.com/docs/reference/android/com/google/firebase/auth/AuthCredential



private FirebaseAuth firebaseAuth;


private FirebaseAuth firebaseAuth;


firebaseAuth = FirebaseAuth.getInstance();


//logging in the user
firebaseAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
progressDialog.dismiss();
//if the task is successfull
if(task.isSuccessful()){
//start the activity
finish();
startActivity(new Intent(getApplicationContext(), Activity.class));
}
}
});



For email :


AuthCredential credential = EmailAuthProvider
.getCredential("user@example.com", "password1234");



For google you need the GoogleSignInAccount idToken docs


AuthCredential credential = GoogleAuthProvider
.getCredential(acct.getIdToken(), null);



For facebook you need facebook AccessToken getToken() docs


AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());



try this Authcredential au = new Authcredential();


Authcredential au = new Authcredential();






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift