Support automatic OAuth redirect for Web Application clients

This commit is contained in:
Antigravity 2026-06-13 23:05:53 -04:00
parent dcb6f952cc
commit db78f0c8ba
2 changed files with 41 additions and 3 deletions

View File

@ -219,7 +219,9 @@ app.post('/api/auth/url', (req, res) => {
const SCOPES = ['https://www.googleapis.com/auth/gmail.readonly', 'https://www.googleapis.com/auth/gmail.modify'];
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
prompt: 'consent',
scope: SCOPES,
state: accountName // pass the accountName in state so we don't lose it across redirects
});
res.json({ success: true, url: authUrl });

View File

@ -31,6 +31,43 @@ export default function GmailAgentUI() {
useEffect(() => {
fetchAccounts();
// Check for OAuth redirect
const params = new URLSearchParams(window.location.search);
const codeParam = params.get('code');
const stateParam = params.get('state');
if (codeParam && stateParam) {
setAuthStep(2);
setAuthCode(codeParam);
setNewAccountName(stateParam);
// Auto-submit the token verification
const verifyRedirect = async () => {
try {
const res = await fetch("/proxy/gmail/api/auth/token", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ accountName: stateParam.trim(), code: codeParam.trim() })
});
const data = await res.json();
if (data.success) {
alert(`Account '${stateParam}' added successfully!`);
setAuthStep(0);
setNewAccountName("");
setAuthCode("");
window.history.replaceState({}, document.title, window.location.pathname);
fetchAccounts();
} else {
alert("OAuth verification failed: " + data.error);
}
} catch (err) {
alert("Error verifying OAuth: " + err.message);
}
};
verifyRedirect();
}
}, []);
const handleGenerateUrl = async (e) => {
@ -166,12 +203,11 @@ export default function GmailAgentUI() {
<ol style={{ marginLeft: "20px", marginBottom: "24px", color: "var(--text-secondary)", lineHeight: "1.6" }}>
<li>Click the link below to open Google's secure login page.</li>
<li>Sign in with your Gmail account and allow the requested permissions.</li>
<li>Copy the verification code provided by Google.</li>
<li>Paste it into the box below.</li>
<li>If you are not automatically redirected back, copy the verification code provided by Google and paste it below.</li>
</ol>
<div style={{ background: "rgba(0,0,0,0.3)", padding: "16px", borderRadius: "8px", marginBottom: "24px", wordBreak: "break-all" }}>
<a href={authUrl} target="_blank" rel="noopener noreferrer" style={{ color: "var(--accent-cyan)", textDecoration: "none" }}>{authUrl}</a>
<a href={authUrl} style={{ color: "var(--accent-cyan)", textDecoration: "none" }}>{authUrl}</a>
</div>
<form onSubmit={handleVerifyCode} style={{ display: "flex", flexDirection: "column", gap: "16px" }}>