KMPAuth
2.5.0-alpha01indexedEnables seamless authentication with Google, Apple, and Github using Firebase. Facilitates integration through customizable sign-in buttons and provides comprehensive multiplatform support for mobile applications.
Enables seamless authentication with Google, Apple, and Github using Firebase. Facilitates integration through customizable sign-in buttons and provides comprehensive multiplatform support for mobile applications.
Simple and easy to use Kotlin Multiplatform Authentication library targeting iOS, Android, Desktop and Web (kmpauth-firebase module doesn't have web support yet). Supporting Google, , , authentication integrations using Firebase.
Because I am using KMPAuth in production KMP project, I'll support development of this library :).
Related blog post:
You can check out for full library api information.
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-firebase-facebook:<version>") //Facebook authentication 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))
There is not any platform specific setup in Android side.
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)") }
}
//Facebook button with icon
FacebookButtonUiContainerFirebase(
onResult = { result -> /* handle FirebaseUser result or error */ },
linkAccount = false
) {
FacebookSignInButtonIconOnly(onClick = { this.onClick() })
}
//Icon Only Button
FacebookButtonUiContainerFirebase(
modifier = Modifier.fillMaxWidth().height(dp),
onResult = { result -> },
linkAccount =
) {
FacebookSignInButton(fontSize = sp) { .onClick() }
}
FacebookButtonUiContainerFirebase(
modifier = Modifier.fillMaxWidth().height(dp),
onResult = { result -> },
linkAccount =
) {
YourCustomButton(fontSize = sp) { .onClick() }
}
Add these to your res/values/strings.xml:
<string name="facebook_app_id">YOUR_FACEBOOK_APP_ID</string>
<string name="fb_login_protocol_scheme">fbYOUR_FACEBOOK_APP_ID</string>
<string name="facebook_client_token">YOUR_FACEBOOK_CLIENT_TOKEN</string>
Add these metadata tags and Facebook Activity to your AndroidManifest.xml inside the <application> tag:
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id" />
<meta-data
android:name="com.facebook.sdk.ClientToken"
= />
For Facebook Login, on Your Main Activity's activity result call KMPAuth.handleFacebookActivityResult function:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
KMPAuth.handleFacebookActivityResult(requestCode, resultCode, data)
super.onActivityResult(requestCode, resultCode, data)
}
Add Facebook Login SDK Swift Package, and add below to your Info.plist:
Initialize Facebook SDK on Ios Swift side
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() }
}
//Facebook Sign-In Button and authentication with Firebase
FacebookButtonUiContainer(
onResult = { result -> /* handle FirebaseUser result or error */ },
linkAccount = false
) {
FacebookSignInButton(onClick = { 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)") }
}
}
}
Add clientID, and serverId to your Info.plist file as below:
<key>GIDServerClientID</key>
<string>YOUR_SERVER_CLIENT_ID</string>
<key>GIDClientID</key>
<string>YOUR_IOS_CLIENT_ID</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>YOUR_DOT_REVERSED_IOS_CLIENT_ID</string>
</array>
</dict>
</array>
And finally, you need the code below to implement application delegate function calls on the Swift side.
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)
})
}
}
}
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fbFACEBOOK_APP_ID</string> <!-- Your Facebook App ID with 'fb' prefix -->
</array>
</dict>
</array>
<key>FacebookAppID</key>
<string>FACEBOOK_APP_ID</string>
<key>FacebookClientToken</key>
<string>YOUR_FACEBOOK_CLIENT_TOKEN</string>
<key>FacebookDisplayName</key>
<string>YourAppDisplayName</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
FirebaseApp.configure()
// Initialize Facebook SDK.
FBSDKCoreKit.ApplicationDelegate.shared.application(
application,
didFinishLaunchingWithOptions: launchOptions
)
return true
}
func application(
_ app: UIApplication,
open url: URL,
options: [UIApplication.OpenURLOptionsKey : Any] = [:]
) -> Bool {
var handled: Bool
handled = FBSDKCoreKit.ApplicationDelegate.shared.application(
app,
open: url,
options: options
)
if handled {
return true
}
return false
}
Surfaced from shared tags and platforms — no rankings paid for.