Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase kicks out current user

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?

like image 355
Victor Le Avatar asked May 30 '16 03:05

Victor Le


People also ask

Does Firebase user UID change?

UIDs will never change.

Why Firebase user still signed in after I deleted it from Firebase dashboard?

The underlying ID token that authenticates refreshes every hour. So, after an account is deleted, the client will be authenticated for up to one hour. Only until it tries to refresh the ID token will the client become unauthenticated.

What triggers onAuthStateChanged?

onAuthStateChanged. Adds an observer for changes to the user's sign-in state. Prior to 4.0. 0, this triggered the observer when users were signed in, signed out, or when the user's ID token changed in situations such as token expiry or password change.


1 Answers

Update 20161110 - original answer below

Also, check out this answer for a different approach.

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 which 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 than 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, the main auth is the admin and the second auth is the newly created user.

like image 93
André Kool Avatar answered Sep 23 '22 06:09

André Kool