Support automatic OAuth redirect for Web Application clients
This commit is contained in:
parent
dcb6f952cc
commit
db78f0c8ba
|
|
@ -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 SCOPES = ['https://www.googleapis.com/auth/gmail.readonly', 'https://www.googleapis.com/auth/gmail.modify'];
|
||||||
const authUrl = oAuth2Client.generateAuthUrl({
|
const authUrl = oAuth2Client.generateAuthUrl({
|
||||||
access_type: 'offline',
|
access_type: 'offline',
|
||||||
|
prompt: 'consent',
|
||||||
scope: SCOPES,
|
scope: SCOPES,
|
||||||
|
state: accountName // pass the accountName in state so we don't lose it across redirects
|
||||||
});
|
});
|
||||||
|
|
||||||
res.json({ success: true, url: authUrl });
|
res.json({ success: true, url: authUrl });
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,43 @@ export default function GmailAgentUI() {
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchAccounts();
|
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) => {
|
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" }}>
|
<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>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>Sign in with your Gmail account and allow the requested permissions.</li>
|
||||||
<li>Copy the verification code provided by Google.</li>
|
<li>If you are not automatically redirected back, copy the verification code provided by Google and paste it below.</li>
|
||||||
<li>Paste it into the box below.</li>
|
|
||||||
</ol>
|
</ol>
|
||||||
|
|
||||||
<div style={{ background: "rgba(0,0,0,0.3)", padding: "16px", borderRadius: "8px", marginBottom: "24px", wordBreak: "break-all" }}>
|
<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>
|
</div>
|
||||||
|
|
||||||
<form onSubmit={handleVerifyCode} style={{ display: "flex", flexDirection: "column", gap: "16px" }}>
|
<form onSubmit={handleVerifyCode} style={{ display: "flex", flexDirection: "column", gap: "16px" }}>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue