KotlinMultiplatformAuth
Kotlin Multiplatform Authentication(KMAuth) Library Targeting Android, iOS, Desktop and Web (Kotlin/Js and Kotlin/Wasm both)
Install / Use
/learn @sunildhiman90/KotlinMultiplatformAuthREADME
KotlinMultiplatformAuth - A Kotlin Multiplatform Authentication Library
Kotlin Multiplatform Authentication library targeting Android, iOS, Desktop and Web(Kotlin/Js and Kotlin/Wasm Both). It supports following Sign In Features:
- Sign In with Google(Without 3rd Party Auth Library)
- Sign In With Apple/Facebook/GitHub/Twitter and other OAuthProviders SupabaseOAuthProvider Using Supabase.
- Sign In With Email/Phone/IDToken etc. all SupabaseDefaultAuthProvider Using Supabase.
NOTE: For using Sign In With Apple or other OAuthProviders with SupabaseAuthManager, you need to setup the supabase for specific provider, refer to the supabase docs.
Deepwiki Docs
Youtube Video
Youtube vide for using Google Signin In Compose Multiplatform using KotlinMultiplatformAuth How to Use Google Signin In Compose Multiplatform using KotlinMultiplatformAuth
Quick Start Sample Code
KMAuthInitializer
First of all you need to initialize the KMAuthInitializer.
// For Android platform, Its better to call it from the activity, Becoz we need activity context. We dont need to cal this from other platforms except android.
KMAuthInitializer.initContext(
kmAuthPlatformContext = KMAuthPlatformContext(this)
)
//For Sign In With Google, Then we need to call initialize method from common code
KMAuthInitializer.initialize(KMAuthConfig.forGoogle(webClientId = "YOUR_WEB_CLIENT_ID"))
// For Sign In With Apple or other providers, we need to call KMAuthSupabase.initialize method from common code
KMAuthSupabase.initialize(
config = KMAuthConfig.forSupabase(
supabaseUrl = "YOUR_SUPABASE_URL",
supabaseKey = "YOUR_SUPABASE_KEY",
deepLinkHost = "YOUR_DEEP_LINK_HOST",
deepLinkScheme = "YOUR_DEEP_LINK_SCHEME",
),
redirectUrl = "YOUR_REDIRECT_URL"
)
// Also if you need to provide different redirect urls, you can call KMAuthSupabase.initialize method from platform specific code for each platform.
// Also sometime if supabase does not pickup redirect urls from supabase dashboard, in that case you can provide it in initialize method.
We need webClientId from Google Cloud Platform Console to setup the serverClientId in Google API for identifying signed-in users in backend server.
GoogleAuthManager, AppleAuthManager and SupabaseAuthManager
After initializing the KMAuthInitializer, you can use the KMAuthGoogle object to get the GoogleAuthManager Or KMAuthApple to get the AppleAuthManager Or KMAuthSupabase object to get the SupabaseAuthManager to sign in the user according to your requirement.
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
//Ideally you will be using these from ViewModel or Repo
// For Sign In With Google
val googleAuthManager = KMAuthGoogle.googleAuthManager
// For Sign In With Apple: Available only after 0.3.0
val appleAuthManager = KMAuthApple.appleAuthManager
// For Sign In With other providers such as Facebook, Github, Twitter etc. : Available only after 0.3.0
// For these providers using supabase, for setting up the supabase for specific provider, refer to the supabase docs
val supabaseAuthManager = KMAuthSupabase.getAuthManager()
val scope = rememberCoroutineScope()
Button(onClick = {
scope.launch {
val result = googleAuthManager.signIn()
if (result.isSuccess) {
println("Login Successful user: ${result.getOrNull()}")
userName = result.getOrNull()?.name
} else {
println("Error in google Sign In: ${result.exceptionOrNull()}")
}
}
}) {
Text("Sign In With Google")
}
Button(onClick = {
scope.launch {
val result = appleAuthManager.signIn()
if (result.isSuccess) {
println("Login Successful user: ${result.getOrNull()}")
userName = result.getOrNull()?.name
} else {
println("Error in apple Sign In: ${result.exceptionOrNull()}")
}
}
}) {
Text("Sign In With Apple")
}
Button(onClick = {
scope.launch {
val oauthProvider = SupabaseOAuthProvider.Github
val result = supabaseAuthManager.signInWith(supabaseOAuthProvider = oauthProvider)
if (result.isSuccess) {
println("Login Successful user: ${result.getOrNull()}")
userName = result.getOrNull()?.name
} else {
println("Error in supabase oauth provider $oauthProvider Sign In: ${result.exceptionOrNull()}")
}
}
}) {
Text("Sign In With Github")
}
}
// For all OAuthProviders except Apple, you need to call handleDeepLink from iOSApp file(ie. entry point)
// Call this method in onOpenUrl method of iOSApp file, this is not needed for sign in with apple for iOS
// becoz for iOS we are using AuthenticationServices + supabase signInWithIdToken but not signInWith oauth provider, but on Android, its needed:
ContentView()
.onOpenURL { url in
// Handle supabase deep link url
//If using kmauth-supabase
KMAuthSupabase.shared.deepLinkHandler().handleDeepLinks(url: url)
}
//Similarly for android, call this method in MainActivity, We need Android deep link handling code for Apple oauth provider as well along with other oauth providers of supabase:
override fun onNewIntent(
intent: Intent,
caller: ComponentCaller,
) {
super.onNewIntent(intent, caller)
//If using only kmauth-apple
KMAuthApple.shared.deepLinkHandler().handleDeepLinks(intent)
//If using kmauth-supabase directly
//KMAuthSupabase.shared.deepLinkHandler().handleDeepLinks(intent)
}
NOTE: For deep links setup we can follow these links for android and ios: Android: https://developer.android.com/training/app-links/deep-linking iOS: https://developer.apple.com/documentation/xcode/defining-a-custom-url-scheme-for-your-app Also make sure to add these deep link urls(with this combination -> scheme://host) in Redirect URLs in URL Configuration section of supabase auth dashboard.
You can find these sample codes in the sample
NOTE: To test the sample, Make sure to perform the Platform Setup for Android, ISO, Desktop and Web(Kotlin/Js only) as mentioned in the Platform Setup section.
How to Use in Compose Implementation?
If you want to use GoogleSignIn in Compose Multiplatform, you can use Either the above code
Or After setting up the KMAuthInitializer, you can use directly GoogleSignInButton composable if you
have installed the kmauth-google-compose module.
GoogleSignInButton(modifier = Modifier) { user, error ->
if (error != null) {
println("GoogleSignInButton: Error in google Sign In: $error")
}
if (user != null) {
println("GoogleSignInButton: Login Successful")
println("User Info: ${user.name}, ${user.email}, ${user.profilePicUrl}")
}
}
Features
- Sign In with Google(Without 3rd Party Auth Library)
- Sign In With Apple: iOS(Native Sign in Using AuthenticationServices + Supabase), Android, Jvm, Js and WasmJs(Using Supabase)
- Sign In With Facebook/GitHub/Twitter and other OAuthProviders SupabaseOAuthProvider Using Supabase.
- Sign In With Email/Phone/IDToken etc. all SupabaseDefaultAuthProvider Using Supabase.
Installation
KMPAuth is available on Maven Central. In your root project build.gradle.kts file (or
settings.gradle file), make sure to add mavenCentral() to repositories section.
repositories {
mavenCentral()
}
Then you can add the required library module to your project as follows:
commonMain.dependencies {
// For using in Pure KMP module without compose, We need to add this as api dependency
api("io.github.sunildhiman90:kmauth-google:<version>")
// For using Sign In With Apple, We need to add this as api dependency to handle deep linking from ios
api("io.github.sunildhiman90:kmauth-apple:<version>")
// For using Sign In With OAuthProviders such as Google/Facebook/GitHub/Twitter etc. with SupabaseAuthManager, We need to add this as api dependency to handle deep linking from ios
api("io.github.sunildhiman90:kmauth-supabase:<version>")
//For using Google Sign In in Compose Multiplatform app.
implementation("io.github.sunildhiman90:kmauth-google:<version>")
// Optional: Only if you want to use in built One Tap GoogleSignInButton composable directly
implementation("io.github.sunildhiman90:kmauth-google-compose:<version>")
}
//And for Sign In With Google or other OAuthProviders, you need to export api dependencies using export
listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach { iosTa
