Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always call firebase getIdToken() #264

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class MainActivity : AppCompatActivity() {
val result = auth.signInWithCredential(firebaseCredential)
// Sign in success, update UI with the signed-in user's information
lifecycleScope.launch {
UserData().userRepository.setUser(result.user?.toUser())
UserData().userRepository.setUser(result.user)
println("user id=${result.user?.uid}")
println("idToken=${result.user?.getIdToken(true)}")
}
Expand Down Expand Up @@ -161,3 +161,4 @@ class MainActivity : AppCompatActivity() {
}
}


1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ apollo-adapters = { module = "com.apollographql.apollo3:apollo-adapters" }
apollo-normalized-cache = { module = "com.apollographql.apollo3:apollo-normalized-cache" }
apollo-normalized-cache-sqlite = { module = "com.apollographql.apollo3:apollo-normalized-cache-sqlite" }
apollo-runtime = { module = "com.apollographql.apollo3:apollo-runtime" }
atomicfu = "org.jetbrains.kotlinx:atomicfu:0.20.1"
shared-preferences = "androidx.preference:preference:1.2.1"
coil-compose = "io.coil-kt:coil-compose:2.5.0"
qdsfdhvh-imageloader = { module = "io.github.qdsfdhvh:image-loader", version.ref = "image-loader" }
Expand Down
1 change: 1 addition & 0 deletions shared/data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ kotlin {
implementation(libs.apollo.normalized.cache.sqlite)
implementation(libs.apollo.normalized.cache)

implementation(libs.atomicfu)
implementation(libs.androidx.datastore.preferences.core)
api(libs.androidx.datastore.preferences)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ actual class ApolloClientBuilder(
/**
*
*/
val token = userRepository.getUser()?.idToken
val token = userRepository.getIdToken()
if (token != null) {
addHeader("Authorization", "Bearer $token")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package fr.androidmakers.store.firebase

import dev.gitlive.firebase.Firebase
import dev.gitlive.firebase.auth.FirebaseUser
import dev.gitlive.firebase.auth.auth
import fr.androidmakers.domain.model.User
import fr.androidmakers.domain.repo.UserRepository
import kotlinx.atomicfu.atomic
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch

@OptIn(DelicateCoroutinesApi::class)
class FirebaseUserRepository : UserRepository {
private var currentUser = atomic<FirebaseUser?>(null)
override val user = MutableStateFlow<User?>(null)

init {
GlobalScope.launch {
try {
setUser(Firebase.auth.currentUser?.toUser())
setUser(Firebase.auth.currentUser)
} catch (e: Exception) {
e.printStackTrace()
}
Expand All @@ -27,7 +30,15 @@ class FirebaseUserRepository : UserRepository {
return user.value
}

override suspend fun setUser(user: User?) {
this.user.emit(user)
override suspend fun getIdToken(): String? {
return currentUser.value?.getIdToken(false)
}

override suspend fun setUser(user: Any?) {
check(user is FirebaseUser?) {
"Expected FirebaseUser, got '$user'"
}
currentUser.value = user
this.user.emit(user?.toUser())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import dev.gitlive.firebase.auth.FirebaseUser
import fr.androidmakers.domain.model.User

suspend fun FirebaseUser.toUser(): User {
return User(uid, photoURL, this.getIdToken(false))
return User(uid, photoURL)
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package fr.androidmakers.domain.model

import okio.Buffer
import okio.ByteString.Companion.decodeBase64

data class User(
val id: String,
val photoUrl: String?,
val idToken: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ import kotlinx.coroutines.flow.StateFlow
interface UserRepository {
val user: StateFlow<User?>
fun getUser(): User?
suspend fun setUser(user: User?)
suspend fun getIdToken(): String?
suspend fun setUser(user: Any?)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ import org.koin.core.component.inject
class UserData: KoinComponent {
val userRepository: UserRepository by inject()
}


Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class GoogleSignInViewModel(
try {
val credential = GoogleAuthProvider.getCredential(idToken!!, null)
Firebase.auth.signInWithCredential(credential).await()
UserData().userRepository.setUser(Firebase.auth.currentUser?.toUser())
UserData().userRepository.setUser(Firebase.auth.currentUser)
onSignInSuccess()
} catch (e: FirebaseAuthException) {
Log.w(TAG, "Could not get Firebase auth credential from Google id token", e)
Expand All @@ -53,7 +53,6 @@ private suspend fun FirebaseUser.toUser(): User {
return User(
id = this.uid,
photoUrl = this.photoUrl.toString(),
idToken = this.getIdToken(true).await().token
)
}

Expand Down
Loading