求教:KMM项目中GitLive Firebase SDK及Firebase Auth架构搭建方法
在KMM项目中使用GitLive Firebase SDK搭建Firebase Auth架构
一、项目依赖配置
1. 根项目build.gradle.kts
在plugins块引入GitLive插件,同时配置对应仓库:
plugins { id("dev.gitlive.firebase") version "1.12.0" apply false } dependencyResolutionManagement { repositories { google() mavenCentral() maven("https://maven.gitlive.io") } }
2. 共享模块build.gradle.kts
应用GitLive插件并添加Firebase Auth依赖:
plugins { id("dev.gitlive.firebase") } firebase { auth() } dependencies { implementation("dev.gitlive:firebase-auth:1.12.0") }
3. 平台端配置
- Android:将
google-services.json放置在app/src/main目录,确保app/build.gradle.kts中应用com.google.gms.google-services插件。 - iOS:把
GoogleService-Info.plist添加到Xcode项目主目标,同时在Firebase控制台开启所需的Auth登录方式(如邮箱密码、Google登录等)。
二、Firebase Auth架构搭建(分层设计)
1. 共享层:定义Auth Repository抽象接口
在共享模块创建统一的Auth操作接口,屏蔽平台差异:
interface AuthRepository { suspend fun signInWithEmailAndPassword(email: String, password: String): Result<User> suspend fun createUserWithEmailAndPassword(email: String, password: String): Result<User> suspend fun signOut() fun getCurrentUser(): User? fun authStateChanges(): Flow<User?> } data class User( val id: String, val email: String? )
2. 共享层:实现Auth Repository
基于GitLive Firebase SDK完成接口实现,处理通用Auth逻辑:
class FirebaseAuthRepository : AuthRepository { private val firebaseAuth = Firebase.auth override suspend fun signInWithEmailAndPassword(email: String, password: String): Result<User> { return try { val authResult = firebaseAuth.signInWithEmailAndPassword(email, password) Result.success(mapToUser(authResult.user)) } catch (e: Exception) { Result.failure(e) } } override suspend fun createUserWithEmailAndPassword(email: String, password: String): Result<User> { return try { val authResult = firebaseAuth.createUserWithEmailAndPassword(email, password) Result.success(mapToUser(authResult.user)) } catch (e: Exception) { Result.failure(e) } } override suspend fun signOut() { firebaseAuth.signOut() } override fun getCurrentUser(): User? { return firebaseAuth.currentUser?.let { mapToUser(it) } } override fun authStateChanges(): Flow<User?> { return firebaseAuth.authStateChanges.map { it?.let { mapToUser(it) } } } private fun mapToUser(firebaseUser: dev.gitlive.firebase.auth.User): User { return User( id = firebaseUser.uid, email = firebaseUser.email ) } }
3. 平台层:初始化与依赖注入
- Android:在Application类或依赖注入容器中提供Repository实例:
val authRepository: AuthRepository = FirebaseAuthRepository()
- iOS:在
AppDelegate或SwiftUI的App结构体中初始化实例:
let authRepository = FirebaseAuthRepository()
4. 业务逻辑层:封装Auth业务逻辑
在共享模块的ViewModel中(结合Kotlin Coroutines和Flow)处理业务逻辑:
class AuthViewModel(private val authRepository: AuthRepository) { private val _authState = MutableStateFlow<User?>(authRepository.getCurrentUser()) val authState: StateFlow<User?> = _authState init { authRepository.authStateChanges() .onEach { _authState.value = it } .launchIn(CoroutineScope(Dispatchers.Main)) } suspend fun signIn(email: String, password: String): Result<Unit> { return authRepository.signInWithEmailAndPassword(email, password) .map { } } suspend fun signUp(email: String, password: String): Result<Unit> { return authRepository.createUserWithEmailAndPassword(email, password) .map { } } suspend fun signOut() { authRepository.signOut() } }
三、常见问题处理
- 平台特定逻辑扩展:如需处理iOS生物识别登录这类平台独有逻辑,可使用Kotlin的
expect/actual机制实现。 - 错误处理优化:在Repository层捕获Firebase Auth异常,转换为业务友好的错误类型,避免直接暴露平台异常到UI层。
- iOS Flow适配:使用
KotlinxCoroutinesFlow将Kotlin Flow转换为Swift的AsyncSequence,方便iOS UI层监听Auth状态变化。
内容的提问来源于stack exchange,提问作者lCoinsin
相关产品推荐
相关产品推荐

