iosMain.com.mmk.kmpauth.google.GoogleAuthUiProviderImpl.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kmpauth-google Show documentation
Show all versions of kmpauth-google Show documentation
Kotlin Multiplatform Authentication Library targeting ios and android
package com.mmk.kmpauth.google
import cocoapods.GoogleSignIn.GIDSignIn
import kotlinx.cinterop.ExperimentalForeignApi
import platform.UIKit.UIApplication
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
internal class GoogleAuthUiProviderImpl : GoogleAuthUiProvider {
@OptIn(ExperimentalForeignApi::class)
override suspend fun signIn(filterByAuthorizedAccounts: Boolean): GoogleUser? = suspendCoroutine { continutation ->
val rootViewController =
UIApplication.sharedApplication.keyWindow?.rootViewController
if (rootViewController == null) continutation.resume(null)
else {
GIDSignIn.sharedInstance
.signInWithPresentingViewController(rootViewController) { gidSignInResult, nsError ->
nsError?.let { println("Error While signing: $nsError") }
val user = gidSignInResult?.user
val idToken = user?.idToken?.tokenString
val accessToken = user?.accessToken?.tokenString
val profile = gidSignInResult?.user?.profile
if (idToken != null && accessToken != null) {
val googleUser = GoogleUser(
idToken = idToken,
accessToken = accessToken,
displayName = profile?.name ?: "",
profilePicUrl = profile?.imageURLWithDimension(320u)?.absoluteString
)
continutation.resume(googleUser)
} else continutation.resume(null)
}
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy