How to keep Chrome Extension popup open during Google login with launchWebAuthFlow? #165967
Replies: 3 comments 2 replies
|
Hey there, I ran into the same thing—Chrome extension popups just can’t stay open once I ended up solving it by moving the actual “what happens after login” logic out of the popup and into a background script, then storing the result in And right after I sign in with Supabase, I save the profile: That way, even if the popup closes during the OAuth dance, the next time it re-opens it sees “oh, I’m logged in” and immediately redirects. Alternatively, you can bypass the popup entirely for auth and open a normal tab (or window) for Google sign-in. That tab redirects back to your extension (or a small HTML page you host), and then you message the background script to stash the token—again, the popup stays out of it and never needs to hang around for the redirect. Hope that helps! Let me know if you want a code snippet for the “open a new tab for auth” approach. |
|
Hi @Interview-Prep1, Below is a self-contained example of how to replace your const handleGoogleLogin = async () => {
const manifest = chrome.runtime.getManifest();
// Build the Google OAuth2 URL
const authUrl = new URL('https://accounts.google.com/o/oauth2/v2/auth');
authUrl.searchParams.set('client_id', manifest.oauth2.client_id);
authUrl.searchParams.set('response_type', 'id_token');
authUrl.searchParams.set('scope', manifest.oauth2.scopes.join(' '));
authUrl.searchParams.set('redirect_uri', `https://${chrome.runtime.id}.chromiumapp.org/`);
authUrl.searchParams.set('prompt', 'select_account');
// 1) Open a new browser tab for the OAuth flow
chrome.tabs.create({ url: authUrl.href }, (tab) => {
const authTabId = tab.id;
// 2) Listen for the redirect back to our extension URI
const onUpdated = (tabId, changeInfo) => {
if (
tabId === authTabId &&
changeInfo.url?.startsWith(`https://${chrome.runtime.id}.chromiumapp.org/`)
) {
// Stop listening and close the tab
chrome.tabs.onUpdated.removeListener(onUpdated);
chrome.tabs.remove(authTabId);
// 3) Parse the id_token from the URL fragment
const url = new URL(changeInfo.url);
const params = new URLSearchParams(url.hash.slice(1));
const idToken = params.get('id_token');
if (!idToken) {
console.error('No id_token found in redirect URI');
return;
}
// 4) Sign in with Supabase using the ID token
supabase.auth
.signInWithIdToken({ provider: 'google', token: idToken })
.then(async ({ data, error }) => {
if (error) throw error;
// Fetch session and user profile
const { data: sessionData } = await supabase.auth.getSession();
const { data: profile } = await supabase
.from('user_profiles')
.select('is_premium')
.eq('user_id', sessionData.session.user.id)
.single();
// Dispatch Redux action
dispatch(loginSuccess({
user: sessionData.session.user,
token: sessionData.session.access_token,
isPremium: profile.is_premium,
}));
// 5) Persist to chrome.storage for your useEffect listener
chrome.storage.local.set({
user: {
id: sessionData.session.user.id,
isPremium: profile.is_premium,
accessToken: sessionData.session.access_token,
}
});
setToast({ message: 'Login successful!', type: 'success' });
})
.catch((err) => {
console.error('Supabase login failed:', err.message);
setToast({ message: 'Login failed', type: 'error' });
});
}
};
chrome.tabs.onUpdated.addListener(onUpdated);
});
};Key steps explained:
Let me know if you need any more tweaks! |
|
Think of a Chrome‑extension popup like a soap bubble: the moment you look away—click anywhere outside—it pops. That auto‑close behavior is hard‑wired into the browser, so there’s no secret flag or hack that can keep the popup around. (Stack Overflow) How people dodge the problem1. Run Google sign‑in somewhere that can actually stay open.
2. Persist the outcome. 3. Let the popup pick up where it left off.
Crack those open in separate tabs and you’ll have step‑by‑step examples for every API involved. |



Uh oh!
There was an error while loading. Please reload this page.
Body
I'm using chrome.identity.launchWebAuthFlow() in my Chrome Extension popup to log in users with Google (via Supabase).
Project created in React
The problem is: when i click on the "Continue with Google" button the popup closes automatically after the user picks a Google account. I want to keep the popup open so I can show the next page (/premiumUser or /freeUser) after login.
When i choose the Account for Login then this page close and the extension also close but i want to show the next page after login but the extension will close.
Is there any way to prevent the popup from closing, or is it better to open a separate tab or window for login?
Below is the Code:
` const handleGoogleLogin = async () => {
const manifest = chrome.runtime.getManifest();
const url = new URL('https://accounts.google.com/o/oauth2/auth');
url.searchParams.set('client_id', manifest.oauth2.client_id);
url.searchParams.set('response_type', 'id_token');
url.searchParams.set('access_type', 'offline');
url.searchParams.set('redirect_uri',
https://${chrome.runtime.id}.chromiumapp.org);url.searchParams.set('scope', manifest.oauth2.scopes.join(' '));
url.searchParams.set('prompt', 'select_account'); // 👈 this forces account picker
chrome.identity.launchWebAuthFlow(
{
url: url.href,
interactive: true,
},
async (redirectedTo) => {
if (chrome.runtime.lastError || !redirectedTo) {
console.error('Google login failed:', chrome.runtime.lastError?.message);
setToast({ message: 'Google login failed. Please try again.', type: 'error' });
return;
}
);
};
`
Guidelines
All reactions