Firebase User Log Out Issue
When attempting to add a new user account, you encounter an issue where the current user is automatically logged out and replaced by the new account. This poses a problem for admins adding multiple accounts while remaining signed in.
To resolve this, you can utilize a separate Firebase authentication reference. By creating a second Firebase app and using its reference for user creation, you can avoid signing out the current user:
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!"); // Consider signing out the secondary user if necessary secondaryApp.auth().signOut(); });
By using the secondary app's authentication reference, you can create users without affecting the current user's session. Note that for user creation, an authentication reference is sufficient. However, if you need to write data based on roles or permissions, ensure that you use the appropriate authentication reference (admin or user), depending on the level of access required. Consider this approach when handling user information and permissions in your Firebase application.
The above is the detailed content of How Can I Prevent Automatic Logout When Adding New Firebase Users?. For more information, please follow other related articles on the PHP Chinese website!