Hello :slightly_smiling_face: I have case that I w...
# moko
k
Hello 🙂 I have case that I would like to use TokenFeature with FirebaseAuth, but my problem is that token/token change event could be obtained only from suspend function, so my idea was to keep it’s value in variable. unfortunately HttpClient freezes everything, (i know why). But I would like make to ask what is best approach in that case ? Use some atomics ? or maybe you have some other advices.
Copy code
fun createHttpClient(): HttpClient {
    var token: String? = "" // <- frozen 
    CoroutineScope(Dispatchers.Main).launch {
        token = Firebase.auth.currentUser?.getIdToken(false)
        Firebase.auth.idTokenChanged.collect { user ->
            token = user?.getIdToken(false)
        }
    }
    
    val client = HttpClient {
        install(TokenFeature) {
            tokenHeaderName = "Authorization"
            tokenProvider = object : TokenFeature.TokenProvider {
                override fun getToken(): String{
                    return "Bearer $token" <- frozen 
                }
            }
        }
    }
    return client
}
for now I had working solution with atomic,
Copy code
class FirebaseTokenProvider(): TokenFeature.TokenProvider{

    var firebaseToken = Atomic<String?>("")

    init {
        CoroutineScope(Dispatchers.Main).launch {
            firebaseToken.set(Firebase.auth.currentUser?.getIdToken(false))
            Firebase.auth.idTokenChanged.collect { user ->
                firebaseToken.set(user?.getIdToken(false))
            }
        }
    }

    override fun getToken(): String? {
        return firebaseToken.get()
    }
}
a
hi! yes, your solution with atomic - good. or you can use
MutableStateFlow
- that too can be used between threads. but atomic is better choice (here not needed Flow logic)