Skip to content

MwaiBanda/Authentication

Repository files navigation

Authentication

GitHub license badge badge badge

Multiplatform Firebase Auth Wrapper For Android, iOS & Js. This library can be used either as a KMM dependency or installed as a standalone library for Android via Maven, iOS with the Swift package manager and Js with npm

Usage

The library exposes a kotlin object called Authentication that provides instances of the AuthentionController, a class that makes thread safe asynchronous calls to the AuthenticationService. A class that makes calls to the Kotlin Firebase SDK @gitlive/firebase-kotlin-sdk

Kotlin

Firebase Initilization

   import io.github.mwaibanda.authentication.di.Authentication 
   import io.github.mwaibanda.authentication.di.initialize
        
   override fun onCreate() {
        super.onCreate()
        Authentication.initializeApp(this) // <- Intialize FirebaseApp
    }

Use the Authetication object to access the shared AuthenticationController.

    val auth: AuthenticationController = Authentication.controller

    auth.signInWithEmail(email, password) { authResult ->
        when(authResult) {
            is AuthResult.Success -> {
                onSuccess(authResult.data)
            }
            is AuthResult.Failure -> {
                onFailure(authResult.message)
            }
        }
    }

Swift

import Authentication // Needed when using standalone library

@main
struct iOSApp: App {
    init() {
        Authentication.initialize() // <-- Call `initialize` to init FirebaseApp
    }
    var body: some Scene {
        WindowGroup {
            ContentView()
...

Use the Authetication class to access the shared instance of the AuthenticationController.

    val controller = Authentication.shared.controller
    controller.signInWithEmail(email: email, password: password) { authResult in
        if let user = authResult.data {
            onSuccess(user)
        } else if let error = authResult.message {
            // Handle error
        }
    }
    
...

AuthenticationController

Use the to access the shared instance of the AuthenticationController

interface AuthenticationController {
    fun signInWithEmail(email: String, password: String, onCompletion: (AuthResult<UserResponse>) -> Unit)
    fun signUpWithEmail(email: String, password: String, onCompletion: (AuthResult<UserResponse>) -> Unit)
    fun signInAsGuest(onCompletion: (AuthResult<UserResponse>) -> Unit)
    fun checkAuthAndSignAsGuest(onCompletion: (AuthResult<UserResponse>) -> Unit)
    fun getCurrentUser(onCompletion: (AuthResult<UserResponse>) -> Unit)
    fun isUserSignedIn(onCompletion: (Boolean) -> Unit)
    fun deleteUser()
    fun signOut()
}

UserResponse

Some calls to the AuthenticationController return this response Object

data class UserResponse(
    val uid: String,
    val email: String?,
    val isAnonymous: Boolean
)

Installation

Check the table below for the compatibilty across Kotlin versions

Library Kotlin
1.0.0 1.6.21
1.0.5+ 1.8.21

Android & KMM

Add the repository on your Project-level gradle

allprojects {
    repositories {
        ...
        maven {
          name = "GitHubPackages"
          url = uri("https://maven.pkg.github.com/MwaiBanda/Authentication")
          credentials {
              username = \* GITHUB ACCESS EMAIL *\
              password = \* GITHUB ACCESS TOKEN *\
          }
       }
...    

KMM

On the module-level, add the library as a dependency

kotlin {
    ...
    sourceSets["commonMain"].dependencies {
        api("io.github.mwaibanda:authentication:1.0.5")
    }
}

iOS

Swift Package Manager

File > Swift Packages > Add Package Dependency
Add https://github.com/MwaiBanda/Authentication.git
branch origin

JS

npm

npm i https://github.com/MwaiBanda/authentication

Maintainers

Contributing

Feel free to dive in! Open an issue or submit PRs.

License

MIT license © Mwai Banda