mirzemehdi / KMPAuth

Kotlin Multiplatform Authentication Library targetting android and iOS
https://mirzemehdi.com/KMPAuth/
Apache License 2.0
234 stars 19 forks source link
android authentication google-auth-library google-authentication ios kotlin-multiplatform kotlin-multiplatform-library kotlin-multiplatform-mobile

KMPAuth - Kotlin Multiplatform Authentication Library

Build Kotlin Maven Central

badge-android badge-ios

Simple and easy to use Kotlin Multiplatform Authentication library targeting iOS and Android. Supporting Google, Apple, Github authentication integrations using Firebase.
Because I am using KMPAuth in FindTravelNow production KMP project, I'll support development of this library :).
Related blog post: Integrating Google Sign-In into Kotlin Multiplatform
You can check out Documentation for full library api information.

Sample App and Code

SampleApp

@Composable
fun AuthUiHelperButtonsAndFirebaseAuth(
    modifier: Modifier = Modifier,
    onFirebaseResult: (Result<FirebaseUser?>) -> Unit,
) {
    Column(modifier = modifier,verticalArrangement = Arrangement.spacedBy(10.dp)) {

        //Google Sign-In Button and authentication with Firebase
        GoogleButtonUiContainerFirebase(onResult = onFirebaseResult) {
            GoogleSignInButton(modifier = Modifier.fillMaxWidth()) { this.onClick() }
        }

        //Apple Sign-In Button and authentication with Firebase
        AppleButtonUiContainer(onResult = onFirebaseResult) {
            AppleSignInButton(modifier = Modifier.fillMaxWidth()) { this.onClick() }
        }

        //Github Sign-In with Custom Button and authentication with Firebase
        GithubButtonUiContainer(onResult = onFirebaseResult) {
            Button(onClick = { this.onClick() }) { Text("Github Sign-In (Custom Design)") }
        }

    }
}

You can check out more sample codes here.

Features

Installation

KMPAuth is available on Maven Central. In your root project build.gradle.kts file (or settings.gradle file) add mavenCentral() to repositories.

repositories { 
  mavenCentral()
}

Then in your shared module add desired dependencies in commonMain. Latest version: Maven Central.

sourceSets {
  commonMain.dependencies {
    implementation("io.github.mirzemehdi:kmpauth-google:<version>") //Google One Tap Sign-In 
    implementation("io.github.mirzemehdi:kmpauth-firebase:<version>") //Integrated Authentications with Firebase
    implementation("io.github.mirzemehdi:kmpauth-uihelper:<version>") //UiHelper SignIn buttons (AppleSignIn, GoogleSignInButton)

  }
}

You will also need to include Google Sign-In and/or FirebaseAuth library to your ios app using Swift Package Manager or Cocoapods.

Note: If in iOS you get MissingResourceException, I wrote solution in this issue's comment section.


Google Sign-In

For Google Sign-In you can either use only one-tap sign in functionality, or also implementing firebase google authentication integration to that. You need to set up OAuth 2.0 in Google Cloud Platform Console. For steps you can follow this link. Pro Easy Tip: If you use Firebase and enable Google Sign-In authentication in Firebase it will automatically generate OAuth client IDs for each platform, and one will be Web Client ID which will be needed for identifying signed-in users in backend server.

Platform Setup

Create GoogleAuthProvider instance by providing Web Client Id as a serverID on Application start.

GoogleAuthProvider.create(credentials = GoogleAuthCredentials(serverId = WebClientId))
Android ##### Android Setup There is not any platform specific setup in Android side.
iOS ##### iOS Setup Add clientID, and serverId to your `Info.plist` file as below: ``` GIDServerClientID YOUR_SERVER_CLIENT_ID GIDClientID YOUR_IOS_CLIENT_ID CFBundleURLTypes CFBundleURLSchemes YOUR_DOT_REVERSED_IOS_CLIENT_ID ``` And finally, you need the code below to implement application delegate function calls on the Swift side. ```swift import SwiftUI import shared import GoogleSignIn class AppDelegate: NSObject, UIApplicationDelegate { func application( _ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:] ) -> Bool { var handled: Bool handled = GIDSignIn.sharedInstance.handle(url) if handled { return true } // Handle other custom URL types. // If not handled by this app, return false. return false } } @main struct iOSApp: App { @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate var body: some Scene { WindowGroup { ContentView().onOpenURL(perform: { url in GIDSignIn.sharedInstance.handle(url) }) } } } ```

Usage

After configuring above steps this is how you can use:

//Google Sign-In with Custom Button (only one tap sign-in functionality)
GoogleButtonUiContainer(onGoogleSignInResult = { googleUser ->
  val idToken = googleUser?.idToken // Send this idToken to your backend to verify
}) {
  Button(onClick = { this.onClick() }) { Text("Google Sign-In(Custom Design)") }
}

Google Sign-In Button and authentication with Firebase. You need to implement kmpauth-uihelper dependency

GoogleButtonUiContainerFirebase(onResult = onFirebaseResult) {
  GoogleSignInButton(modifier = Modifier.fillMaxWidth()) { this.onClick() }
}

Google Sign-In IconOnly Button and authentication with Firebase. You need to implement kmpauth-uihelper dependency

GoogleButtonUiContainerFirebase(onResult = onFirebaseResult) {
  GoogleSignInButtonIconOnly(onClick = { this.onClick() })
}

Apple Sign-In

After enabling and configuring Apple Sign-In in Firebase, make sure you added "Sign In with Apple" capability in XCode. Then, you can use it as below in your @Composable function:

//Apple Sign-In with Custom Button and authentication with Firebase
AppleButtonUiContainer(onResult = onFirebaseResult) {
  //Any View, you just need to delegate child view's click to this UI Container's click method
  Button(onClick = { this.onClick() }) { Text("Apple Sign-In (Custom Design)") }
}

Apple Sign-In with AppleSignInButton. You need to implement kmpauth-uihelper dependency

AppleButtonUiContainer(onResult = onFirebaseResult) {
  AppleSignInButton(modifier = Modifier.fillMaxWidth()) { this.onClick() }
}

Apple Sign-In IconOnly Button. You need to implement kmpauth-uihelper dependency

AppleButtonUiContainer(onResult = onFirebaseResult) {
  AppleSignInButtonIconOnly(onClick = { this.onClick() })
}

Github Sign-In

After enabling and configuring Github Sign-In in Firebase, you can use it as below in your @Composable function:

//Github Sign-In with Custom Button and authentication with Firebase
GithubButtonUiContainer(onResult = onFirebaseResult) {
  //Any View, you just need to delegate child view's click to this UI Container's click method
  Button(onClick = { this.onClick() }) { Text("Github Sign-In (Custom Design)") }
}