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.
@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.
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: .
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.
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.
Create GoogleAuthProvider instance by providing Web Client Id as a serverID on Application start.
GoogleAuthProvider.create(credentials = GoogleAuthCredentials(serverId = WebClientId))
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() })
}
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() })
}
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)") }
}