fix: google account login - 2

This commit is contained in:
Rahul Patel
2026-05-23 02:29:17 +05:30
parent 1607529bbd
commit ada4e8de2a
3 changed files with 29 additions and 26 deletions

View File

@@ -40,8 +40,16 @@ import com.aurora.store.viewmodel.auth.AuthViewModel
private const val EMBEDDED_SETUP_URL = "https://accounts.google.com/EmbeddedSetup"
private const val AUTH_TOKEN = "oauth_token"
private const val JS_PROFILE_EMAIL =
"(function() { return document.getElementById('profileIdentifier').innerHTML; })();"
// Google's EmbeddedSetup post-login page renders the account as e.g.
// <div data-profile-identifier data-email="user@gmail.com">...</div>;
// the legacy id="profileIdentifier" selector no longer matches.
private const val JS_PROFILE_EMAIL = """
(function() {
var el = document.querySelector('[data-profile-identifier][data-email]');
return el ? el.getAttribute('data-email') : null;
})();
"""
@Composable
fun GoogleLoginScreen(
@@ -120,16 +128,12 @@ fun GoogleLoginScreen(
webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView, url: String) {
val cookies = CookieManager.getInstance().getCookie(url)
if (cookies != null) {
val cookieMap = AC2DMUtil.parseCookieString(cookies)
if (cookieMap.isNotEmpty() && cookieMap[AUTH_TOKEN] != null) {
val oauthToken = cookieMap[AUTH_TOKEN]
view.evaluateJavascript(JS_PROFILE_EMAIL) { result ->
val email = result.replace("\"", "")
viewModel.buildAuthData(view.context, email, oauthToken)
}
}
val cookies = CookieManager.getInstance().getCookie(url) ?: return
val cookieMap = AC2DMUtil.parseCookieString(cookies)
val oauthToken = cookieMap[AUTH_TOKEN] ?: return
view.evaluateJavascript(JS_PROFILE_EMAIL) { result ->
val email = result.trim('"')
viewModel.buildAuthData(view.context, email, oauthToken)
}
}
}

View File

@@ -27,11 +27,7 @@ import okhttp3.RequestBody.Companion.toRequestBody
class AC2DMTask @Inject constructor(private val httpClient: HttpClient) {
@Throws(Exception::class)
fun getAC2DMResponse(email: String?, oAuthToken: String?): Map<String, String> {
if (email == null || oAuthToken == null) {
return mapOf()
}
fun getAC2DMResponse(email: String, oAuthToken: String): Map<String, String> {
val params: MutableMap<String, Any> = hashMapOf()
params["lang"] = Locale.getDefault().toString().replace("_", "-")
params["google_play_services_version"] = PLAY_SERVICES_VERSION_CODE
@@ -60,7 +56,7 @@ class AC2DMTask @Inject constructor(private val httpClient: HttpClient) {
return if (response.isSuccessful) {
AC2DMUtil.parseResponse(String(response.responseBytes))
} else {
mapOf()
emptyMap()
}
}

View File

@@ -39,13 +39,13 @@ import com.aurora.store.util.PackageUtil
import com.aurora.store.util.Preferences
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import java.net.ConnectException
import java.net.UnknownHostException
import javax.inject.Inject
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import java.net.ConnectException
import java.net.UnknownHostException
import javax.inject.Inject
@HiltViewModel
class AuthViewModel @Inject constructor(
@@ -92,16 +92,19 @@ class AuthViewModel @Inject constructor(
}
}
fun buildAuthData(context: Context, email: String, oauthToken: String?) {
fun buildAuthData(context: Context, email: String, oauthToken: String) {
viewModelScope.launch(Dispatchers.IO) {
try {
val response = aC2DMTask.getAC2DMResponse(email, oauthToken)
if (response.isNotEmpty()) {
val aasToken = response["Token"]
if (aasToken != null) {
Preferences.putString(context, Constants.ACCOUNT_EMAIL_PLAIN, email)
val accountEmail = response["Email"]?.takeIf { it.isNotBlank() } ?: email
Preferences.putString(context, Constants.ACCOUNT_EMAIL_PLAIN, accountEmail)
Preferences.putString(context, Constants.ACCOUNT_AAS_PLAIN, aasToken)
AuroraApp.events.send(AuthEvent.GoogleLogin(true, email, aasToken))
AuroraApp.events.send(
AuthEvent.GoogleLogin(true, accountEmail, aasToken)
)
} else {
Preferences.putString(context, Constants.ACCOUNT_EMAIL_PLAIN, "")
Preferences.putString(context, Constants.ACCOUNT_AAS_PLAIN, "")
@@ -191,8 +194,8 @@ class AuthViewModel @Inject constructor(
context,
Preferences.PREFERENCE_AUTH_VIA_MICROG,
accountType == AccountType.GOOGLE &&
tokenType == AuthHelper.Token.AUTH &&
PackageUtil.hasSupportedMicroGVariant(context)
tokenType == AuthHelper.Token.AUTH &&
PackageUtil.hasSupportedMicroGVariant(context)
)
_authState.value = AuthState.SignedIn
} else {