fix: resolve Google sign-in processing hang with clean GSI + Token Client OAuth2 pipeline

This commit is contained in:
Adam Eehan committed 2026-11-03 16:46:32 +05:30
1 parent 7dca9c598b
commit bcaa944caf
1 file changed
+97 -52
+97 -52
View File
@@ -1108,24 +1108,20 @@
Sign in with your official Google account to share your feedback. Your verified Google photo and Gmail will automatically link to your review.
</p>
<!-- Real Official Google Sign In Button (Google Identity Services) -->
<div id="g_id_onload"
data-client_id="267802159841-leun1b8b85ongsoc5pkjhufe4s5jbjj8.apps.googleusercontent.com"
data-context="signin"
data-ux_mode="popup"
data-callback="handleGoogleCredentialResponse"
data-auto_prompt="false">
</div>
<div class="flex justify-center items-center my-4 min-h-[50px]">
<div id="gsi-btn-container" class="g_id_signin flex justify-center"
data-type="standard"
data-shape="pill"
data-theme="outline"
data-text="signin_with"
data-size="large"
data-logo_alignment="left"
data-width="280">
</div>
<!-- Real Official Google Sign In Container -->
<div class="flex flex-col items-center justify-center my-4">
<div id="gsi-btn-container" class="min-h-[50px] flex justify-center items-center"></div>
<!-- Direct Token Client Fallback for Mobile/FedCM/White-screen bypass -->
<button
type="button"
onclick="triggerGoogleOAuth()"
class="mt-3 text-xs text-blue-600 hover:text-blue-800 font-medium inline-flex items-center gap-1.5 transition-colors px-3 py-1.5 rounded-lg hover:bg-blue-50"
title="Direct Google Account Selector"
>
<i class="fab fa-google text-[11px]"></i>
<span>Direct Google Account Sign-In</span>
</button>
</div>
<div class="mt-4 flex items-center justify-center gap-2 text-xs text-slate-400">
@@ -3439,42 +3435,16 @@ response = client.chat.completions.create(
loadReviews();
}
// Initialize Google Identity Services
function initGoogleIdentity() {
if (window.google && google.accounts && google.accounts.id) {
try {
google.accounts.id.initialize({
client_id: GOOGLE_CLIENT_ID,
callback: handleGoogleCredentialResponse,
auto_select: false,
cancel_on_tap_outside: true
});
// Render official Google button inside container
const btnContainer = document.getElementById("gsi-btn-container");
if (btnContainer) {
google.accounts.id.renderButton(btnContainer, {
theme: "outline",
size: "large",
type: "standard",
shape: "pill",
text: "continue_with",
logo_alignment: "left",
width: 280
});
}
} catch(e) {
console.log("GSI init note:", e);
}
} else {
// Retry after GSI script finishes loading
setTimeout(initGoogleIdentity, 500);
}
}
let tokenClient = null;
// Handle real Google JWT Credential Response
function handleGoogleCredentialResponse(response) {
window.handleGoogleCredentialResponse = function(response) {
console.log("Google Credential Response received:", response);
try {
if (!response || !response.credential) {
console.error("No credential in response", response);
return;
}
// Decode standard JWT
const base64Url = response.credential.split('.')[1];
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
@@ -3490,13 +3460,88 @@ response = client.chat.completions.create(
};
localStorage.setItem("aira_google_user", JSON.stringify(currentUser));
renderUserAuthUI();
renderUserAuthUI();
} catch(err) {
console.error("Failed to decode Google token:", err);
alert("Google sign-in completed, but failed to parse profile. Please try again.");
}
};
// Initialize Google Identity Services & OAuth 2.0 Token Client
function initGoogleIdentity() {
if (window.google && google.accounts) {
try {
// 1. Initialize Google Identity Services Credential API
if (google.accounts.id) {
google.accounts.id.initialize({
client_id: GOOGLE_CLIENT_ID,
callback: window.handleGoogleCredentialResponse,
auto_select: false,
cancel_on_tap_outside: true,
itp_support: true
});
const btnContainer = document.getElementById("gsi-btn-container");
if (btnContainer) {
btnContainer.innerHTML = "";
google.accounts.id.renderButton(btnContainer, {
theme: "outline",
size: "large",
type: "standard",
shape: "pill",
text: "signin_with",
logo_alignment: "left",
width: 280
});
}
}
// 2. Initialize OAuth 2.0 Token Client (Direct Account Picker - bypasses FedCM/white-screen)
if (google.accounts.oauth2) {
tokenClient = google.accounts.oauth2.initTokenClient({
client_id: GOOGLE_CLIENT_ID,
scope: 'email profile openid',
callback: async (tokenResponse) => {
if (tokenResponse && tokenResponse.access_token) {
try {
const res = await fetch("https://www.googleapis.com/oauth2/v3/userinfo", {
headers: { Authorization: `Bearer ${tokenResponse.access_token}` }
});
if (res.ok) {
const profile = await res.json();
currentUser = {
name: profile.name || "Google User",
email: profile.email,
picture: profile.picture || `https://api.dicebear.com/7.x/initials/svg?seed=${encodeURIComponent(profile.name || "User")}`
};
localStorage.setItem("aira_google_user", JSON.stringify(currentUser));
renderUserAuthUI();
}
} catch(err) {
console.error("UserInfo fetch error:", err);
}
}
}
});
}
} catch(e) {
console.log("GSI init note:", e);
}
} else {
setTimeout(initGoogleIdentity, 300);
}
}
window.triggerGoogleOAuth = function() {
if (tokenClient) {
tokenClient.requestAccessToken({ prompt: 'select_account' });
} else if (window.google && google.accounts && google.accounts.id) {
google.accounts.id.prompt();
} else {
alert("Google Services loading... please wait a moment and try again.");
}
};
function renderUserAuthUI() {
const loggedOutBox = document.getElementById("google-feedback-loggedout");
const loggedInBox = document.getElementById("google-feedback-loggedin");