Firstly, 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.

Common (commonMain sourceSet)

We will create one data class for holding authenticated Google User properties, and the most important one will be idToken field which will be used to authenticate user in backend side.

data class GoogleUser(
val idToken: String,
val displayName: String = "",
val profilePicUrl: String? = null,

And another data class for holding required credential parameters.

data class GoogleAuthCredentials(val serverId: String) //Web client ID

Then we will create two interfaces, one will contain UI layer functionalities (a.k.a Sign-In ), and another will be related to Data layer.

interface GoogleAuthUiProvider {

* Opens Sign In with Google UI,
* @return returns GoogleUser
suspend fun signIn(): GoogleUser?

To ensure that the GoogleAuthUIProvider implementation is accessible only on the UI side and to leverage Compose Multiplatform for the UI, we will use the Composable annotation for returning GoogleAuthUIProvider in the second interface.

interface GoogleAuthProvider {
fun getUiProvider(): GoogleAuthUiProvider

suspend fun signOut()

Next step is to create platform specific implementation of these interfaces.

Android (androidMain sourceSet)

In build.gradle.kts, we need to add the following Google Sign-In dependencies for the androidMain dependencies.

#Google Sign In

GoogleAuthUiProvider implementation ->

internal class GoogleAuthUiProviderImpl(
private val activityContext: Context,
private val credentialManager: CredentialManager,
private val credentials: GoogleAuthCredentials,
) :
GoogleAuthUiProvider {
override suspend fun signIn(): GoogleUser? {
return try {
val credential = credentialManager.getCredential(
context = activityContext,
request = getCredentialRequest()
} catch (e: GetCredentialException) {
AppLogger.e("GoogleAuthUiProvider error: ${e.message}")
} catch (e: NullPointerException) {

private fun getGoogleUserFromCredential(credential: Credential): GoogleUser? {
return when {
credential is CustomCredential && credential.type == GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL -> {
try {
val googleIdTokenCredential =
idToken = googleIdTokenCredential.idToken,
displayName = googleIdTokenCredential.displayName ?: "",
profilePicUrl = googleIdTokenCredential.profilePictureUri?.toString()
} catch (e: GoogleIdTokenParsingException) {
AppLogger.e("GoogleAuthUiProvider Received an invalid google id token response: ${e.message}")

else -> null

private fun getCredentialRequest(): GetCredentialRequest {
return GetCredentialRequest.Builder()
.addCredentialOption(getGoogleIdOption(serverClientId = credentials.serverId))

private fun getGoogleIdOption(serverClientId: String): GetGoogleIdOption {
return GetGoogleIdOption.Builder()

GoogleAuthProvider Implementation ->

internal class GoogleAuthProviderImpl(
private val credentials: GoogleAuthCredentials,
private val credentialManager: CredentialManager,
) : GoogleAuthProvider {

override fun getUiProvider(): GoogleAuthUiProvider {
val activityContext = LocalContext.current
return GoogleAuthUiProviderImpl(
activityContext = activityContext,
credentialManager = credentialManager,
credentials = credentials

override suspend fun signOut() {

iOS (iosMain sourceSet)

In iOS, you also need to add Google Sign-In dependencies. If you use CocoaPods, you can add them as shown below, or you can simply add the library using Swift Package Manager from Xcode.


And add client and server IDs to the Info.plist file.



GoogleAuthUiProvider implementation ->

internal class GoogleAuthUiProviderImpl : GoogleAuthUiProvider {
override suspend fun signIn(): GoogleUser? = suspendCoroutine { continutation ->

val rootViewController =

if (rootViewController == null) continutation.resume(null)
else {
.signInWithPresentingViewController(rootViewController) { gidSignInResult, nsError ->
nsError?.let { println("Error While signing: $nsError") }
val idToken = gidSignInResult?.user?.idToken?.tokenString
val profile = gidSignInResult?.user?.profile
if (idToken != null) {
val googleUser = GoogleUser(
idToken = idToken,
displayName = profile?.name ?: "",
profilePicUrl = profile?.imageURLWithDimension(320u)?.absoluteString
} else continutation.resume(null)



GoogleAuthProvider implementation ->

internal class GoogleAuthProviderImpl :
GoogleAuthProvider {

override fun getUiProvider(): GoogleAuthUiProvider = GoogleAuthUiProviderImpl()

override suspend fun signOut() {


You only need the code below to implement application delegate function calls on the Swift side.

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


struct iOSApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate

var body: some Scene {
WindowGroup {
ContentView().onOpenURL(perform: { url in

Finally, we bring all implementation classes together using either just ‘expect/actual’ or you can use it with the Koin DI framework, as I did in FindTravelNow. I’ve written more detailed blog post about the usage of Koin in Kotlin Multiplatform that you can check out:

Up to this point, our code is actually ready to use. However, we can add a little touch to make our lives easier, allowing us to use this button container across any project, handling all the heavy lifting for us. For each project, we can customize the button however we want.

interface GoogleButtonUiContainerScope {
fun onClick()

fun GoogleButtonUiContainer(
modifier: Modifier = Modifier,
onGoogleSignInResult: (GoogleUser?) -> Unit,
content: @Composable GoogleButtonUiContainerScope.() -> Unit,
) {
val googleAuthProvider = koinInject<GoogleAuthProvider>()
val googleAuthUiProvider = googleAuthProvider.getUiProvider()
val coroutineScope = rememberCoroutineScope()
val uiContainerScope = remember {
object : GoogleButtonUiContainerScope {
override fun onClick() {
coroutineScope.launch {
val googleUser = googleAuthUiProvider.signIn()
modifier = modifier,
content = { uiContainerScope.content() }


Then we simply delegate our button or view click to this container’s click, which will perform Google One Tap Sign-In and notify our screen about the result (our GoogleUser object). Using the ID token, we can send it to our backend server to check the authentication of the user. Finally, this is how simple it is to use it in your views.

GoogleButtonUiContainer(onGoogleSignInResult = { googleUser ->
val idToken=googleUser.idToken // Send this idToken to your backend to verify
}) {
onClick = { this.onClick() }
) {
Text("Sign-In with Google")

You can check out full source code in this PR changes:

Source link