Here is a tested solution you can apply (just implemented a few minutes before).
For creating a new user account you need the reference of FirebaseAuth.
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();
try { FirebaseApp myApp = FirebaseApp.initializeApp(getApplicationContext(), firebaseOptions, "AnyAppName");
mAuth2 = FirebaseAuth.getInstance(myApp);
} catch (IllegalStateException e){
mAuth2 = FirebaseAuth.getInstance(FirebaseApp.getInstance("AnyAppName"));
}
//..... 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();
}
// ...
}
});
}
The point where you have to worry(actually not):
The admin should only be able to create the new user accounts. But the above solutions is allowing all authenticated user to create a new user account.
So to solve this problem you can take help of your firebase real-time database. Just add a key like "is_user_admin" and set the value as true from the console itself. You just need to validate the user before someone is trying to create a new user account. And using this approach you can set your own admin.
As of now, I don't think there is firebase-admin SDK for android. So one can use the above approach.