Skip to content

Commit

Permalink
try refreshing google tokens if they are rejected for being expired
Browse files Browse the repository at this point in the history
  • Loading branch information
frett committed Oct 4, 2023
1 parent 596a718 commit 8977617
Showing 1 changed file with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.core.content.edit
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.common.api.ApiException
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject
import javax.inject.Singleton
Expand All @@ -20,6 +21,9 @@ import org.cru.godtools.account.AccountType
import org.cru.godtools.account.provider.AccountProvider
import org.cru.godtools.api.AuthApi
import org.cru.godtools.api.model.AuthToken
import timber.log.Timber

private const val TAG = "GoogleAccountProvider"

private const val PREFS_GOOGLE_ACCOUNT_PROVIDER = "org.godtools.account.google"
private const val PREF_USER_ID_PREFIX = "user_id_"
Expand Down Expand Up @@ -52,18 +56,32 @@ internal class GoogleAccountProvider @Inject constructor(
state.activity.startActivity(googleSignInClient.signInIntent)
}

private suspend fun refreshSignIn() = try {
googleSignInClient.silentSignIn().await()
} catch (e: ApiException) {
Timber.tag(TAG).d(e, "Error refreshing google account authentication")
null
}

override suspend fun logout() {
googleSignInClient.signOut().await()
}
// endregion Login/Logout

override suspend fun authenticateWithMobileContentApi(): AuthToken? {
val account = GoogleSignIn.getLastSignedInAccount(context)
val request = account?.idToken?.let { AuthToken.Request(googleIdToken = it) } ?: return null
val token = authApi.authenticate(request).takeIf { it.isSuccessful }
?.body()?.takeUnless { it.hasErrors() }
?.dataSingle
if (token != null) prefs.edit { putString(PREF_USER_ID(account), token.userId) }
var account = GoogleSignIn.getLastSignedInAccount(context)
var resp = authenticateWithMobileContentApi(account)

if (resp?.code() == 400) {
account = refreshSignIn()
resp = authenticateWithMobileContentApi(account)
}

val token = resp?.takeIf { it.isSuccessful }?.body()?.takeUnless { it.hasErrors }?.dataSingle
if (account != null && token != null) prefs.edit { putString(PREF_USER_ID(account), token.userId) }
return token
}

private suspend fun authenticateWithMobileContentApi(account: GoogleSignInAccount?) =
account?.idToken?.let { authApi.authenticate(AuthToken.Request(googleIdToken = it)) }
}

0 comments on commit 8977617

Please sign in to comment.