Firebase kicks out current user

2018-12-31 06:28发布

So I have this issue where every time I add a new user account, it kicks out the current user that is already signed in. I read the firebase api and it said that "If the new account was created, the user is signed in automatically" But they never said anything else about avoiding that.

      //ADD EMPLOYEES
      addEmployees: function(formData){
        firebase.auth().createUserWithEmailAndPassword(formData.email, formData.password).then(function(data){
          console.log(data);
        });
      },

I'm the admin and I'm adding accounts into my site. I would like it if I can add an account without being signed out and signed into the new account. Any way i can avoid this?

11条回答
美炸的是我
2楼-- · 2018-12-31 06:56

I faced the same problem, and I solved it with this way:

When the user login, I save the email and password in the shared preferences. And after creating the user, I login the user again with email and password that I have saved before.

    String currentEmail = MyApp.getSharedPreferences().getEmail();
    String currentPass = MyApp.getSharedPreferences().getPass();

    FirebaseAuth auth = FirebaseAuth.getInstance();
    auth.createUserWithEmailAndPassword(email, pass)
            .addOnCompleteListener(AddStudent.this, new OnCompleteListener<AuthResult>() {

                @Override
                public void onComplete(@NonNull final Task<AuthResult> task) {

                    if (task.isSuccessful()) {
                        String currentEmail = MyApp.getSharedPreferences().getEmail();
                        String currentPass = MyApp.getSharedPreferences().getPass();

                        //Sign in again
                        auth.signInWithEmailAndPassword(currentEmail, currentPass)
                                .addOnCompleteListener(AddStudent.this, new OnCompleteListener<AuthResult>() {
                                    @Override
                                    public void onComplete(@NonNull Task<AuthResult> task) {
                                        if (!task.isSuccessful()) {
                                            Log.e("RELOGIN", "FAILED");
                                        } else {
                                            Log.e("RELOGIN", "SUCCESS");
                                        }
                                    }
                                });

                        finish();
                    }
                }
    });
查看更多
高级女魔头
3楼-- · 2018-12-31 06:57

Update 20161110 - original answer below

Also check out this answer for a different aproach.

Original answer

This is actually possible.

But not directly, the way to do it is to create a second auth reference and use that to create users:

var config = {apiKey: "apiKey",
    authDomain: "projectId.firebaseapp.com",
    databaseURL: "https://databaseName.firebaseio.com"};
var secondaryApp = firebase.initializeApp(config, "Secondary");

secondaryApp.auth().createUserWithEmailAndPassword(em, pwd).then(function(firebaseUser) {
    console.log("User " + firebaseUser.uid + " created successfully!");
    //I don't know if the next statement is necessary 
    secondaryApp.auth().signOut();
});

If you don't specify wich firebase connection you use for an operation it will use the first one by default.

Source for multiple app references.

EDIT

For the actual creation of a new user it doesn't matter that there is nobody, or someone else then the admin, authenticated on the second auth reference because for creating an account all you need is the auth reference itself.

The following hasn't been tested but it is something to think about

The thing you do have to think about is writing data to firebase. Common practice is that users can edit/update their own user info so when you use the second auth reference for writing this should work. But if you have something like roles or permissions for that user make sure you write that with the auth reference that has the right permissions. In this case main auth is the admin and second auth is the newly created user.

查看更多
零度萤火
4楼-- · 2018-12-31 07:02

I had a similar problem, so I asked the question in the Firebase Slack Community. I have implemented this, and it works like a charm.

Try this

查看更多
人间绝色
5楼-- · 2018-12-31 07:06

If you are using Polymer and Firebase (polymerfire) see this answer: https://stackoverflow.com/a/46698801/1821603

Essentially you create a secondary <firebase-app> to handle the new user registration without affecting the current user.

查看更多
步步皆殇っ
6楼-- · 2018-12-31 07:09

Update for Swift 4

I have tried a few different options to create multiple users from a single account, but this is by far the best and easiest solution.

Original answer by Nico

First Configure firebase in your AppDelegate.swift file

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    FirebaseApp.configure()
    FirebaseApp.configure(name: "CreatingUsersApp", options: FirebaseApp.app()!.options)

    return true
}

Add the following code to action where you are creating the accounts.

            if let secondaryApp = FirebaseApp.app(name: "CreatingUsersApp") {
                let secondaryAppAuth = Auth.auth(app: secondaryApp)

                // Create user in secondary app.
                secondaryAppAuth.createUser(withEmail: email, password: password) { (user, error) in
                    if error != nil {
                        print(error!)
                    } else {
                        //Print created users email.
                        print(user!.email!)

                        //Print current logged in users email.
                        print(Auth.auth().currentUser?.email ?? "default")

                        try! secondaryAppAuth.signOut()

                    }
                }
            }
        }
查看更多
几人难应
7楼-- · 2018-12-31 07:10

The Swift version:

FIRApp.configure()

// Creating a second app to create user without logging in
FIRApp.configure(withName: "CreatingUsersApp", options: FIRApp.defaultApp()!.options)

if let secondaryApp = FIRApp(named: "CreatingUsersApp") {
    let secondaryAppAuth = FIRAuth(app: secondaryApp)
    secondaryAppAuth?.createUser(...)
}
查看更多
登录 后发表回答