StuartDTO
StuartDTO

Reputation: 1031

Android : Firebase current user returning null even if I've logged in before

I'm implementing SignIn with Google on my app, the sign it works, but now I'm planning to on my SplashActivity check if someone is logged in to the app or not to show either the login or the home page. So I've planned like this :

This is my ViewModel:

class SplashViewModel @Inject constructor(
    private val authenticatedUserRepository: AuthenticatedUserRepository
) : ViewModel() {

    private val _splashViewState =
        MutableLiveData<SplashViewState>()
    val splashViewState: LiveData<SplashViewState>
        get() = _splashViewState

    private lateinit var userAuthenticated: LiveData<FirebaseUser>

    fun checkIfUserIsAuthenticatedInFirebase() {
        userAuthenticated = authenticatedUserRepository.isLogged()
        if (userAuthenticated.value == null) {
            _splashViewState.postValue(SplashViewState.UserNotAuthenticated)
        } else {
            _splashViewState.postValue(SplashViewState.UserAuthenticated)

        }
    }

}

sealed class SplashViewState {
    object UserAuthenticated : SplashViewState()
    object UserNotAuthenticated : SplashViewState()
}

And this is my repository

interface AuthenticatedUserRepository {
    fun isLogged(): MutableLiveData<FirebaseUser>
}

class AuthenticatedUserRepositoryImpl @Inject constructor(private val firebaseAuth: FirebaseAuth) :
    AuthenticatedUserRepository {

    override fun isLogged(): MutableLiveData<FirebaseUser> {
        val authenticatedUser: MutableLiveData<FirebaseUser> =
            MutableLiveData()
        val currentUser = firebaseAuth.currentUser
        if (currentUser != null) authenticatedUser.postValue(currentUser)
        else authenticatedUser.postValue(null)
        return authenticatedUser
    }
}

And the thing is always I'm returning null, even if I've logged in and closed the app and open again.

I've also read the medium post from @AlexMamo but he's using Firestore to check if the user is registered or not, is it possible to use my own backend to make it free?

Upvotes: 0

Views: 246

Answers (1)

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20626

You do not need to return a MutableLiveData<FirebaseUser>, you can just return the FirebaseUser and on your view-model, you just check if that's null or not.

fun checkIfUserIsAuthenticatedInFirebase() {
        val firebaseUser = authenticatedUserRepository.isLogged()
        if (firebaseUser == null) {
            _splashViewState.postValue(SplashViewState.UserNotAuthenticated)
        } else {
            _splashViewState.postValue(SplashViewState.UserAuthenticated)
        }
    }

And your Repository

interface AuthenticatedUserRepository {
    fun isLogged(): FirebaseUser?
}

class AuthenticatedUserRepositoryImpl @Inject constructor(private val firebaseAuth: FirebaseAuth) :
    AuthenticatedUserRepository {

    override fun isLogged() = firebaseAuth.currentUser
}

And it should work.

Upvotes: 1

Related Questions