Dmitriy1892
Dmitriy1892

Reputation: 33

Firebase.auth.currentUser is never null

I need to receive the "null" value from the Firebase.auth.currentUser function, but it returns some default user.

val auth = Firebase.auth
val currentUser = auth.currentUser
if (currentUser != null) {
        val action = SignInFragmentDirections.actionSignInFragmentToTransactionsFragment(currentUser)
        findNavController().navigate(action)
    }

Since the auth.currentUser never returns null, my application always goes to another screen and does not allow registering a new user. Why is this happening and how can I get null when calling auth.сurrentUser?

Upvotes: 1

Views: 1438

Answers (2)

Dmitriy1892
Dmitriy1892

Reputation: 33

I figured it out - if I logged in app and didn't call the auth.signOut method - I deleted the account from the Firebase Console only, for this reason the auth token saved locally for a some time and for this reason auth.currentUser returned non-null. When I deleted the app (and added the signOut logic in my app later) and reinstall it, all works correct.

Upvotes: 2

Dharmaraj
Dharmaraj

Reputation: 50850

"Does not allow registering a new user"

It seems to me you are trying to create multiple users from using the Android SDK but it is not built for that. Once you sign in, then you would have to sign out to create another user because only one user can be logged in a single application (at least of a single Firebase project).

If you want to create multiple accounts then you can do so directly from the Firebase console. However if your app is meant to be used by admin or privileged users who can create new accounts and you don't want to give them access to the Firebase console then you would have to use something like Cloud functions or your own server.

There you can use the Firebase Admin SDK to create new users. A simple functions may look like:

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

You can call the function from your app like this:

private fun createUser(name: String, email: String): Task<String> {
    // Create the arguments to the callable function.
    val data = hashMapOf(
        "name" to name,
        "email" to email
    )

    return functions
            .getHttpsCallable("createNewUser")
            .call(data)
            .continueWith { task ->
                val result = task.result?.data as String
            }
}

You can read more about callable functions here.

Do note that I simply ran the create user method in the cloud functions but ideally you should check which user is calling the function (context.auth has the info) and make sure only the authorized user can use that function.

Upvotes: 1

Related Questions