Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase create user without sign in [duplicate]

I wish to create a new user account from my application when logged in as a "admin user", The issue is i just want to create it not actually sign in.

is it possible to disable the automatic sign in when creating a new user with the email / password.

I see others have asked this question in relation to JS and swift but can not seem to find any android specific related info. i'm trying to achieve the same thing as this person but with android

any help appreciated

like image 579
RoRo88 Avatar asked Feb 04 '17 11:02

RoRo88


People also ask

How do you prevent simultaneous logins of the same user with Firebase?

Set the rules of your database We want the user not to be able to add a connected device if the value is above 2. This is ensured by the following rules. The user will only be able to write on the database if the value he sent is under or equals 2, meaning they can only have 2 simultaneous connections.

Can we use Firebase without authentication?

No Firebase Authentication…To use the Firebase Storage we need to authenticate a user via Firebase authentication. The default security rules require users to be authenticated. Firebase Storage is basically a powerful and simple object storage, in which you can store your files easily.

Are users in Firebase unique?

Firebase users have a fixed set of basic properties—a unique ID, a primary email address, a name and a photo URL—stored in the project's user database, that can be updated by the user (iOS, Android, web).


1 Answers

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.

like image 75
Amit Upadhyay Avatar answered Sep 27 '22 23:09

Amit Upadhyay