Last active
August 7, 2025 13:20
-
-
Save arulwastaken/4ab0623634790d7d95854f8a48e57f74 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codingwitharul.bill_it.common.auth | |
import android.content.Context | |
import androidx.credentials.CredentialManager | |
import androidx.credentials.CustomCredential | |
import androidx.credentials.GetCredentialRequest | |
import androidx.credentials.exceptions.GetCredentialException | |
import androidx.credentials.exceptions.NoCredentialException | |
import com.codingwitharul.bill_it.common.model.GoogleUser | |
import com.codingwitharul.bill_it.domain.repo.AuthRepo | |
import com.google.android.libraries.identity.googleid.GetGoogleIdOption | |
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential | |
import com.google.firebase.auth.FirebaseAuth | |
import com.google.firebase.auth.GoogleAuthProvider | |
import kotlinx.coroutines.CoroutineScope | |
import kotlinx.coroutines.tasks.await | |
actual class GoogleAuthenticator(val credentialManager: CredentialManager, val context: Context) { | |
actual suspend fun login(): Result<GoogleUser?> { | |
try { | |
val googleIdOption = GetGoogleIdOption.Builder() | |
.setServerClientId("<YOUR SECRET CODE>") | |
.setAutoSelectEnabled(false) | |
.setFilterByAuthorizedAccounts(false) | |
.build() | |
val request = GetCredentialRequest.Builder() | |
.addCredentialOption(googleIdOption) | |
.build() | |
val result = credentialManager.getCredential(context, request) | |
val credential = result.credential | |
if (credential is CustomCredential && credential.type == GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL) { | |
val cred = GoogleIdTokenCredential.createFrom(credential.data) | |
val idToken = cred.idToken | |
val authCredential = GoogleAuthProvider.getCredential(idToken, null) | |
val fireBase = FirebaseAuth.getInstance() | |
val user = fireBase.signInWithCredential(authCredential).await().user | |
return Result.success(GoogleUser( | |
name = cred.displayName, | |
phoneNumber = cred.phoneNumber, | |
profilePictureUrl = cred.profilePictureUri.toString(), | |
token = idToken, | |
email = user?.email, | |
uid = user?.uid!! | |
)) | |
} | |
return Result.failure("No Google ID token found".toThrowable()) | |
} catch (e: NoCredentialException) { | |
e.printStackTrace() | |
return Result.failure("No email found in your device".toThrowable()) | |
} catch (e: GetCredentialException) { | |
e.printStackTrace() | |
return Result.failure("GetCredentialException".toThrowable()) | |
} catch (e: Exception) { | |
return Result.failure(e.message.toString().toThrowable()) | |
} | |
} | |
} | |
private fun String.toThrowable(): Throwable { | |
return Exception(this) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codingwitharul.bill_it.common.auth | |
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.platform.LocalContext | |
import androidx.credentials.CredentialManager | |
import com.google.firebase.auth.FirebaseAuth | |
actual class GoogleAuthProvider(val credentialManager: CredentialManager) { | |
@Composable | |
actual fun getUiProvider(): GoogleAuthenticator { | |
val activityContext = LocalContext.current | |
return GoogleAuthenticator(credentialManager, activityContext) | |
} | |
actual suspend fun signOut() { | |
FirebaseAuth.getInstance().signOut() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment