diff --git a/gmail-agent/index.js b/gmail-agent/index.js index c88615d..0c2c1cd 100644 --- a/gmail-agent/index.js +++ b/gmail-agent/index.js @@ -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 }); diff --git a/interface/hq-dashboard/components/GmailAgentUI.jsx b/interface/hq-dashboard/components/GmailAgentUI.jsx index fe5c9b1..03040ef 100644 --- a/interface/hq-dashboard/components/GmailAgentUI.jsx +++ b/interface/hq-dashboard/components/GmailAgentUI.jsx @@ -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() {
  1. Click the link below to open Google's secure login page.
  2. Sign in with your Gmail account and allow the requested permissions.
  3. -
  4. Copy the verification code provided by Google.
  5. -
  6. Paste it into the box below.
  7. +
  8. If you are not automatically redirected back, copy the verification code provided by Google and paste it below.
- {authUrl} + {authUrl}