0

I am trying to develop a login OAuth system in the google chrome extension, notwithstanding, The main window is been disappeared when I attempt to get the response back from the callback.

// background.js

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
    if (request.message === 'login') {
        console.log("Login success - ");
        user_signed_in = true;
        sendResponse('success');


        chrome.identity.launchWebAuthFlow({
            // url: `https://discord.com/api/oauth2/authorize?client_id=937186052412678185&permissions=8&redirect_uri=https%3A%2F%2Focdecabdmpigonkcbncodnpgepjjnehm.chromiumapp.org%2F&response_type=code&scope=identify%20email%20guilds%20bot`,
            url: DISCORD_URI,
            interactive: true
        }, function (redirect_uri) {
            // console.log("Background - ", redirect_uri);
            if (chrome.runtime.lastError || redirect_uri.includes('access_denied')) {
                console.log("Could not authenticate.");
                sendResponse('fail');
            } else {
                console.log("Login success - ");
                user_signed_in = true;
                sendResponse('success');

            }

            sendResponse('success');

        });
        /*
        */


        return true;
    }
});

Full code

MD SHAYON
  • 7,001
  • 45
  • 38
  • 1
    Assuming the main window means the popup, it may be automatically closed, so instead of sendResponse use chrome.runtime.sendMessage to send to a background script, and continue the process there. To show the result you can open a new window using chrome.windows.create. – wOxxOm Jan 30 '22 at 15:46
  • Yes, it's popup. Thank you for your valuable instruction that I will try and let you know. thank you – MD SHAYON Jan 30 '22 at 15:59

0 Answers0