2024-11-17 20:07:42 +08:00
|
|
|
package com.aiosman.ravenow.utils
|
2024-08-23 21:03:43 +08:00
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import androidx.credentials.Credential
|
|
|
|
|
import androidx.credentials.CredentialManager
|
|
|
|
|
import androidx.credentials.CustomCredential
|
|
|
|
|
import androidx.credentials.GetCredentialRequest
|
|
|
|
|
import androidx.credentials.GetCredentialResponse
|
|
|
|
|
import com.google.android.libraries.identity.googleid.GetGoogleIdOption
|
|
|
|
|
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun handleGoogleSignIn(result: GetCredentialResponse, onLoginWithGoogle: (String) -> Unit) {
|
|
|
|
|
val credential: Credential = result.credential
|
|
|
|
|
|
|
|
|
|
if (credential is CustomCredential) {
|
|
|
|
|
if (GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL.equals(credential.type)) {
|
|
|
|
|
val googleIdTokenCredential: GoogleIdTokenCredential =
|
|
|
|
|
GoogleIdTokenCredential.createFrom(credential.data)
|
|
|
|
|
onLoginWithGoogle(googleIdTokenCredential.idToken)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
suspend fun GoogleLogin(context: Context, onLoginWithGoogle: (String) -> Unit) {
|
|
|
|
|
val credentialManager = CredentialManager.create(context)
|
|
|
|
|
val googleIdOption: GetGoogleIdOption = GetGoogleIdOption.Builder()
|
2024-09-09 22:55:10 +08:00
|
|
|
.setServerClientId("987156664714-3bpf58ldhhr0m474ep48l668ngdn7860.apps.googleusercontent.com")
|
|
|
|
|
.setFilterByAuthorizedAccounts(false)
|
2024-08-23 21:03:43 +08:00
|
|
|
.build()
|
|
|
|
|
val request = GetCredentialRequest.Builder().addCredentialOption(googleIdOption)
|
|
|
|
|
.build()
|
|
|
|
|
|
|
|
|
|
credentialManager.getCredential(context, request).let {
|
|
|
|
|
handleGoogleSignIn(it, onLoginWithGoogle)
|
|
|
|
|
}
|
|
|
|
|
}
|