Kotlin Multiplatform Authentication library targeting Android, iOS, Desktop and Web(Kotlin/Js only). Currently supports Sign In with Google. Planning to add other providers in the future.
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.
KMAuthInitializer.initWithContext(
webClientId = "YOUR_WEB_CLIENT_ID",
kmAuthPlatformContext = KMAuthPlatformContext(this)
)
//For other platforms except android we can use this without context
KMAuthInitializer.init(webClientId = "YOUR_WEB_CLIENT_ID")
We need webClientId from Google Cloud Platform Console to setup the serverClientId in Google API for identifying signed-in users in backend server.
After initializing the KMAuthInitializer, you can use the KMAuthGoogle object to get the GoogleAuthManager object to sign in the user.
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
//Ideally you will be using it from ViewModel or Repo
val googleAuthManager = KMAuthGoogle.googleAuthManager
val scope = rememberCoroutineScope()
Button(onClick = {
scope.launch {
googleAuthManager.signIn { user, error ->
if (error != null) {
println("Error in google Sign In: $error")
}
if (user != null) {
println("Login Successful user: $user")
}
}
}
}) {
Text("Google Sign In")
}
}
You can get the googleAuthManager from the KMAuthGoogle object and use it to sign in the user.
You can find the sample code 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.
If you want to use it 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}")
}
}
- One Tap Sign In with Google(Without Firebase)
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 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>")
}
NOTE: You need to add Google Sign-In for iOS dependency in your xcode project.
KotlinMultiplatformAuth has the following modules:
- kmauth-core: Core module, dont need to add as dependency, already included in other modules as dependency
- kmauth-google: Pure Kotlin Multiplatform module, If you want to use in kotlin multiplatform without compose multiplatform. This can be used in compose multiplatform as well.
- kmauth-google-compose: Compose implementation with in built GoogleSignInButton composable. If you want to use in kotlin multiplatform with compose multiplatform.
First of all you need to setup OAuth 2.0 in your project using any of the given methods.
First of all, you need to set up OAuth 2.0 in Google Cloud Platform Console to get the web Client ID and Client Secret.
You can follow this link to setup OAuth 2.0 in Google Cloud Platform Console.
Once you have set up OAuth 2.0, you need to get the Client ID and Client Secret from Google Cloud Platform Console and add it to your project.
You can setup OAuth 2.0 in Firebase as well. If you Enable Google as a sign-in method in the Firebase console: In the Firebase console, open the Auth section. On the Sign in method tab, enable the Google sign-in method and click Save.
Once you have enabled Google sign-in in Firebase, Firebase will automatically generate OAuth client IDs for each platform for your app and add in Google Cloud Platform Console for your firebase project. You can get the web Client ID from there from Web client from oauth clients
In this case you can skip the step of setting up OAuth 2.0 in Google Cloud Platform Console. Becoz it will be handled by Firebase.
After setting up OAuth 2.0 in Google Cloud Platform Console, you dont need any specific setup for Android except calling the KMAuthInitializer.initWithContext method as mentioned below.
KMAuthInitializer.initWithContext(
webClientId = "YOUR_WEB_CLIENT_ID",
kmAuthPlatformContext = KMAuthPlatformContext(this)
)
Alternative Way (Recommended Way for Compose Multiplatform): Alternatively you can use the KMAuthInitializer.initContext method from android MainActivity, if you want to setup webClientId from common code for all platforms(App composable).
KMAuthInitializer.initContext(
kmAuthPlatformContext = KMAuthPlatformContext(this)
)
And then setup webClientId in common code for all platforms(App composable).
KMAuthInitializer.init(
webClientId = "YOUR_WEB_CLIENT_ID",
)
If you use This Alternative Way, then you dont need to initialize the KMAuthInitializer in each platform except Android and JVM. You just need to call initContext in Android and need to call initClientSecret in deskstop/jvm source set.
After setting up OAuth 2.0 in Google Cloud Platform Console, you need to call the KMAuthInitializer.init method with webClientId if you are not using Alternative Way mentioned in Android setup. If you are using Alternative Way, then you don't need to call the KMAuthInitializer.init method from iOS becoz we will call it from common code.
Additional Setup: For iOS, we need to do some additional setups as well for Google Sign-In for iOS.
- You need to add following code in your Info.plist file.
<key>GIDClientID</key><string>YOUR_IOS_CLIENT_ID</string>
<key>CFBundleURLTypes</key><array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>YOUR_IOS_REVERSED_CLIENT_ID</string>
</array>
</dict>
</array>
Suppose your iOS client Id is :
xyzzzzz.apps.googleusercontent.com
Then your YOUR_REVERSED_CLIENT_ID value will be:
com.googleusercontent.apps.xyzzzzz
**NOTE:** You can get your YOUR_IOS_CLIENT_ID and YOUR_REVERSED_CLIENT_ID from
GoogleService-Info.plist
or from Google Cloud Platform Console for iOS oauth client detail.
From ios oauth client detail, you can get your Client ID as YOUR_IOS_CLIENT_ID and iOS URL scheme as
YOUR_REVERSED_CLIENT_ID from additional information section.
- You also need to add Google Sign-In for iOS Swift Package
dependency in your xcode project, Otherwise you will get this error:
ld: warning: Could not find or use auto-linked framework 'GoogleSignIn': framework 'GoogleSignIn' not found
- On Desktop/Jvm platform, you need to call the KMAuthInitializer.initClientSecret method with clientSecret which you can get from web client id. IF you go to the web client id detail and check additional information, It will show you client secret there. Make sure to call this method before calling App composable in main.kt in desktopMain source set.
KMAuthInitializer.initClientSecret(
clientSecret = OAUTH_CLIENT_SECRET,
)
- You also need to make sure you have added the redirect url in web client in google cloud platform oauth clients in Authorized redirect URIs: http://localhost:8080/callback, And this may take some time to reflect updates in your code while running your code.
You need to call the MAuthInitializer.init method with webClientId if you are not using Alternative Way mentioned in Android setup. If you are using Alternative Way, then you don't need to call the KMAuthInitializer.init method from web becoz we will call it from common code.
IMP:
Also make sure you have added your allowed origin urls in : Authorized JavaScript origins section in
Google Cloud Platform Console for web auth client, Otherwise you will get this error:
Access blocked, Authorization error: The request is not allowed.
For example if you are running on localhost:8080, add this: http://localhost:8080, IN case of your production url you need to add your prod url.