Firebase: Automated Login After User Creation
In Firebase, creating a new user account typically results in the current user being automatically signed out. This issue poses a challenge for administrators adding new accounts while remaining signed in.
The solution proposed by the Firebase API refers to automatic login only after a new account is created. However, the documentation lacks guidance on preventing this behavior.
Solution: Utilizing a Secondary Authentication Reference
To avoid automatic login and retain the current user's session, it is necessary to employ a second authentication reference. This reference can be used specifically for creating new users without affecting the existing session.
<br>const config = {apiKey: "...", authDomain: "...", databaseURL: "..."};<br>const secondaryApp = firebase.initializeApp(config, "Secondary");</p> <p>secondaryApp.auth().createUserWithEmailAndPassword(email, password).then(function(firebaseUser) {<br> console.log("User " firebaseUser.uid " created successfully!");<br>});<br>
Considerations for Data Writing
While this method prevents automated login, it introduces other considerations. When writing data to Firebase, it is crucial to use the appropriate authentication reference based on user permissions. For example, if data updates are restricted to specific users, the second authentication reference should be used to preserve user-specific permissions.
The above is the detailed content of How Can I Prevent Automatic Login After User Creation in Firebase?. For more information, please follow other related articles on the PHP Chinese website!