(1) I'm creating an extension that works with a website that requires a user log in.
(2) I'm trying to detect if the user is logged in or not, from the extension's popup.
I can tell if the user is logged in or not by making a fetch call to specific URL, let's say http://www.example.com/account
, and what happens is:
If the account is logged IN, the fetch call is redirected to
http://www.example.com/account/data
.If the account is logged OUT, the fetch call is redirected to
http://www.example.com/login
.
The problem is:
I do the fetch call from the extension's content script and it works just fine, but, when I do it from inside the extension's popup, there comes two scenarios:
(1) If I'm logging into the website in a normal window, the fetch call works just fine, it detects that I'm logging in and redirects me to http://www.example.com/account/data
.
(2) But, if I'm NOT logging into the website in a normal window and instead logging into it in an incognito window, it does NOT detect my login and it always redirects the fetch call to the http://www.example.com/login
page!
I was thinking maybe it has something to do with the website's cookies, so, I tried to get the cookies, using the chrome.cookies
API, and set them to the extension's popup, but, this didn't work!
Here is how I was trying to do it:
popup.js
chrome.cookies.getAll({
domain: 'example.com'
}, function (cookies) {
cookies.forEach(function (cookie, i) {
// tried to set it this way, it didn't work:
chrome.cookies.set(cookie, function (savedCookie) {});
// tried to set it to the `document.cookie`, as suggested here:
// https://stackoverflow.com/a/59620840/7607751
// but, this didn't work either:
document.cookie = `${cookie.name}=${cookie.value}; `;
});
onAfterCookiesHandle();
});
function onAfterCookiesHandle() {
console.log('document.cookie:', document.cookie);
fetch(`https://www.example.com/account/`, {
method: 'GET',
credentials: 'include',
}).then(function (response) {
console.log('response:', response);
if (response.url === `https://www.example.com/account/data`) {
console.log('You are logged in, yay!');
} else {
console.log('You are logged out, oh no!');
}
}).catch(function (error) {
console.error("HTTP fetch error:", error);
});
}
PS. I'm enabling the extension to work with the incognito mode
(from the extension's settings), and in my manifest.json
file I have the key: "incognito": "spanning"
.
What am I doing wrong here? and how can I get this to work in the incognito mode ?