I want Admins to create users. But this will be on the client side as there will be many admins and I don't want that many people to have access to my firebase console. That's why I don't want to use Firebase-Admin SDK.
I have searched many places and found this. Below is that code:
So you can create two different FirebaseAuth objects like:
private FirebaseAuth mAuth1;
private FirebaseAuth mAuth2;
Now in the onCreate you can initialize them as:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mAuth1 = FirebaseAuth.getInstance();
FirebaseOptions firebaseOptions = new FirebaseOptions.Builder()
.setDatabaseUrl("[Database_url_here]")
.setApiKey("Web_API_KEY_HERE")
.setApplicationId("PROJECT_ID_HERE").build();
FirebaseApp myApp = FirebaseApp.initializeApp(getApplicationContext(),firebaseOptions,
"AnyAppName");
mAuth2 = FirebaseAuth.getInstance(myApp);
//..... other code here
}
To get ProjectID, WebAPI key you can go to Project Settings in your firebase project console.
Now to create the user account you have to use mAuth2, not mAuth1. And then on successful registration, you can log out that mAuth2 user.
Example:
private void createAccount(String email, String password)
{
mAuth2.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
String ex = task.getException().toString();
Toast.makeText(RegisterActivity.this, "Registration Failed"+ex,
Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(RegisterActivity.this, "Registration successful",
Toast.LENGTH_SHORT).show();
mAuth2.signOut();
}
// ...
}
});
}
But here I am able to create users but only once. After that I am not able to reopen that activity again. It is showing "FirebaseApp name AnyAppName already exists!".