Firebase create unsigned user

I want to create a new user account from my application at login as "admin user". The problem is that I just want it to not really be.

you can turn off the automatic sign when creating a new user using email / password.

I see that others have asked this question in relation to JS and quickly, but cannot find any specific Android-related information. I'm trying to achieve the same as this person , but with android

any help appreciated

+6
source share
1 answer

Here is a test solution that you can apply (just implemented a few minutes before).

To create a new user account, you need a FirebaseAuth link.

So you can create two different FirebaseAuth objects, for example:

 private FirebaseAuth mAuth1; private FirebaseAuth mAuth2; 

Now in 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 the ProjectID key, WebAPI, you can go to the project settings in the firebase project console.

Now to create a user account you should use mAuth2 , not mAuth1 . And then, upon successful registration, you can log out of this mAuth2 user.

An 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 you have to worry about (actually not):

The administrator must be able to create new user accounts. But the above solutions allow all authenticated users to create a new user account.

So, to solve this problem, you can use your database in the firebase database. Just add a key like " is_user_admin " and set the value to true from the console itself. You just need to verify the user before someone tries to create a new user account. And using this approach, you can set your own administrator.

At the moment, I do not think there is a firebase-admin SDK for Android. Thus, the above approach can be used.

+4
source

Source: https://habr.com/ru/post/1014697/


All Articles