Skip to content

πŸ”₯ Production-ready Firebase Authentication for Kotlin Multiplatform (Android, iOS, Desktop) - Zero-config, type-safe, with unified API

License

Notifications You must be signed in to change notification settings

com3run/firebase-auth-kmp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

12 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Firebase Auth KMP

Maven Central GitHub Release License Kotlin

Android iOS Desktop GitHub Stars

πŸ”₯ Production-Ready Firebase Authentication for Kotlin Multiplatform πŸ”₯

A complete Firebase Authentication solution for Android, iOS, and Desktop
with a unified, type-safe API. Zero-config on Android, one-line setup on iOS!

πŸ“¦ Installation β€’ πŸ“– Docs β€’ 🎯 Examples β€’ ⭐ Star Us!


✨ Features

  • 🎯 Cross-platform: Single codebase for Android, iOS & Desktop (JVM)
  • πŸš€ Easy Integration: Auto-initialization on Android, one-line setup on iOS
  • πŸ” Complete Authentication:
    • Email/Password (Sign up & Sign in)
    • Google Sign-In
    • Apple Sign-In (iOS)
    • Anonymous Authentication
    • Facebook Sign-In
  • πŸ”„ Real-time Auth State: Flow-based auth state monitoring
  • πŸ›‘οΈ Type-safe: Kotlin-first API with sealed classes for results
  • πŸ§ͺ Testable: Includes FakeAuthBackend for unit testing
  • πŸ“± Platform-optimized: Native Firebase SDKs on Android/iOS, REST API on Desktop
  • πŸ’Ž Production-ready: Published on Maven Central

πŸš€ Quick Start

⚑ Super fast? See QUICKSTART.md (30 seconds)

πŸ“– Detailed setup: See below (2 minutes)

Installation

From Maven Central (Recommended)

kotlin {
    sourceSets {
        commonMain.dependencies {
            implementation("dev.com3run:firebase-auth-kmp:1.0.3")
        }
    }
}

From JitPack (Alternative)

For JitPack, if you have a jvm("desktop") target, use platform-specific dependencies:

repositories {
    maven("https://jitpack.io")
}

kotlin {
    sourceSets {
        androidMain.dependencies {
            implementation("com.github.com3run.firebase-auth-kmp:firebase-auth-kmp-android:v1.0.3")
        }
        iosMain.dependencies {
            implementation("com.github.com3run.firebase-auth-kmp:firebase-auth-kmp-iosarm64:v1.0.3")
        }
        val desktopMain by getting {
            dependencies {
                implementation("com.github.com3run.firebase-auth-kmp:firebase-auth-kmp-jvm:v1.0.3")
            }
        }
    }
}

Note: JitPack requires platform-specific artifacts for desktopMain source sets. For automatic resolution, use Maven Central instead.

Platform Setup

Android βœ… AUTOMATIC!

No code needed! Just add your google-services.json file.

❌ OLD: You had to manually set ActivityHolder.current βœ… NEW: Auto-initializes via ContentProvider!

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // That's it! No Firebase Auth initialization needed! πŸŽ‰
        setContent {
            App()
        }
    }
}

iOS πŸ“± ONE LINE!

  1. Copy FirebaseAuthBridge.swift from the library to your iOS app
  2. Add to AppDelegate:
import FirebaseCore

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        FirebaseApp.configure()
        FirebaseAuthBridge.shared.start()  // ← ONE LINE!
        return true
    }
}

Get the bridge file β†’

Desktop πŸ’» SIMPLE CONFIG!

Create firebase-config.json in your project root:

{
  "apiKey": "YOUR_FIREBASE_API_KEY",
  "projectId": "your-project-id"
}

Find your API key in Firebase Console β†’


🎯 Want detailed setup? See EASY-INTEGRATION.md

πŸ’‘ Usage

Basic Authentication

import dev.com3run.firebaseauthkmp.*
import org.koin.core.context.startKoin

// Initialize Koin
startKoin {
    modules(module {
        single<AuthBackend> { platformAuthBackend() }
        single { AuthRepository(get()) }
    })
}

// Use auth repository
val authRepository = get<AuthRepository>()

// Monitor auth state
authRepository.authState.collect { user ->
    if (user != null) {
        println("Signed in: ${user.email}")
    } else {
        println("Signed out")
    }
}

Sign In with Email/Password

val result = authRepository.signInWithEmailAndPassword(
    email = "user@example.com",
    password = "password123"
)

when (result) {
    is AuthResult.Success -> println("Welcome ${result.data.displayName}!")
    is AuthResult.Failure -> when (result.error) {
        AuthError.InvalidCredential -> println("Wrong email or password")
        AuthError.UserNotFound -> println("No account found")
        else -> println("Error: ${result.error}")
    }
}

Sign In with Google

// Request ID token (platform-specific UI flow)
val idToken = requestGoogleIdToken()

if (idToken != null) {
    val result = authRepository.signInWithGoogle(idToken)
    when (result) {
        is AuthResult.Success -> println("Google sign-in successful!")
        is AuthResult.Failure -> println("Error: ${result.error}")
    }
}

Sign In Anonymously

val result = authRepository.signInAnonymously()

when (result) {
    is AuthResult.Success -> println("Signed in as guest!")
    is AuthResult.Failure -> println("Error: ${result.error}")
}

Sign Out

authRepository.signOut()

πŸ“š Documentation

Getting Started

Platform-Specific

Advanced

πŸ—οΈ Architecture

The library follows clean architecture principles:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚      AuthRepository                 β”‚  ← Validation + High-level API
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               β”‚
               β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚      AuthBackend (Interface)        β”‚  ← Platform-agnostic contract
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               β”‚
       β”Œβ”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
       β–Ό                β–Ό              β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Android     β”‚  β”‚     iOS     β”‚  β”‚   Desktop    β”‚
β”‚  Firebase    β”‚  β”‚  Firebase   β”‚  β”‚  Firebase    β”‚
β”‚  SDK         β”‚  β”‚  Bridge     β”‚  β”‚  REST API    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key Components

  • AuthRepository: High-level API with input validation
  • AuthBackend: Platform-specific interface (Android/iOS/Desktop)
  • AuthModels: Common data models (AuthUser, AuthResult, AuthError)

Platform Implementations

Platform Implementation Features
Android Native Firebase SDK βœ… Auto-init, Full OAuth, Offline support
iOS Notification bridge βœ… Native SDK, Full OAuth, One-line setup
Desktop REST API βœ… Email/Password, Anonymous, Manual OAuth

πŸ§ͺ Testing

The library includes FakeAuthBackend for unit testing:

import dev.com3run.firebaseauthkmp.FakeAuthBackend
import dev.com3run.firebaseauthkmp.AuthRepository

@Test
fun `test sign in success`() = runTest {
    val fakeBackend = FakeAuthBackend()
    val authRepository = AuthRepository(fakeBackend)

    fakeBackend.setAuthResult(AuthResult.Success(testUser))

    val result = authRepository.signInWithEmailAndPassword("test@example.com", "password")

    assertTrue(result is AuthResult.Success)
}

πŸ“¦ Sample App

This repository includes a complete sample app demonstrating all authentication methods:

  • βœ… Email/Password authentication
  • βœ… Google Sign-In
  • βœ… Apple Sign-In (iOS)
  • βœ… Anonymous authentication
  • βœ… Profile management
  • βœ… Password reset

Run the composeApp module to see it in action!

βš™οΈ Requirements

  • Kotlin 2.0+
  • Android API 24+ (Android 7.0)
  • iOS 13.0+
  • JVM 11+ (Desktop)
  • Firebase project with Authentication enabled

πŸ†• What's New in v1.0.3

  • ✨ Desktop/JVM support - Run on Windows, macOS, Linux
  • πŸš€ Android auto-initialization - Zero manual setup required!
  • πŸ“± Simplified iOS setup - Ready-to-use bridge template
  • πŸ“š Easy Integration Guide - Get started in 2 minutes
  • πŸ”§ Better error messages - Clear, actionable feedback

See full changelog β†’

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ”— Links

πŸ‘¨β€πŸ’» Credits

Created by Kamran Mammadov

Special thanks to all contributors!


Made with ❀️ using Kotlin Multiplatform

⭐ If you find this library helpful, please star the repo!

About

πŸ”₯ Production-ready Firebase Authentication for Kotlin Multiplatform (Android, iOS, Desktop) - Zero-config, type-safe, with unified API

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •