Firebase Kicks Out Current User

Firebase kicks out current user

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.

How to avoid being signed in automatically when a new user is created using firebase

There is no direct way provided for web development to create user without signin. Instead there is method to create user without sign-in in Admin-SDK which will solve your problem.

To access this function you have to use node.js in firebase function.

Firebase kicks out current user - Managing users with the web SDK

The mobile and web clients for Firebase Authentication are not capable of changing the status of an arbitrary user account. A user account only has permission to delete itself, not other accounts.

You will need to use the Admin SDK for that, from a backend or desktop system you control. Initialized with a service account, it will have privileged access to manage user accounts.

current user is not getting signed out

When signing out with FirebaseAuth all you need to do is to call FirebaseAuth.instance.signOut(). You don't need to sign out from Google seperately.

How to create user auth without closing the current firebase session

You cannot create new users using client SDK. By that I mean a user creating new users as required. You need to use Firebase Admin SDK (which must be in a secure server environment - like Firebase Cloud Functions).

You can write a cloud function like this:

exports.createNewUser = functions.https.onCall((data, context) => {
if (isAdmin(context.auth.uid)) {
return admin.auth().createUser({
email: data.email,
password: data.password,
displayName: data.name
}).then((userRecord) => {
// See the UserRecord reference doc for the contents of userRecord.
console.log('Successfully created new user:', userRecord.uid);
return { uid: userRecord.uid }
}).catch((error) => {
console.log('Error creating new user:', error);
return { error: "Something went wrong" }
});
}
return {error: "unauthorized" }
})

Now there are multiple ways you could verify that the user who is calling this function is an admin. First one would be using Firebase Custom Claims which are somewhat like roles you assign to users. Another option would be storing UID of using in database and checking the UID exists in admin node of db. Just make sure only you can edit that part of the database.

To call the function from client:

const createNewUser = firebase.functions().httpsCallable('createNewUser');
createNewUser({ name: "test", email: "test@test.test", password: "122345678" })
.then((result) => {
// Read result of the Cloud Function.
var response = result.data;
});

My Firebase app doesn't store current user data locally

When you reload the page Firebase automatically tries to refresh the user's authentication state. But since this requires a call to the server it takes some time, and for that reason the user is initially set to null.

To pick up when Firebase has finished refreshing the user's authentication state, use an auth state listener as shown in the first sample in the documentation on getting the current user:

firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
} else {
// No user is signed in.
}
});

Firebase getting current user uid

No, there is no charge associated with getting current user id or email or any kind of auth information. Firebase auth is completely free except the phone auth. You can have unlimited number of users in Firebase Authentication with email, Google, Microsoft etc.

The charge with reference to the read operations is for Firestore. It is totally separate 'module' than Authentication.

If a user logs out of a Firebase Auth app will it log them out on all of their devices?

No. A signed in user is persistent on a device until they sign out on that device, or the user's auth token gets deleted somehow.



Related Topics



Leave a reply



Submit