Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting whether user is logged in or not from a Chrome extension

is there an easy way to detect from a Chrome extension that the user is logged into his google account or not.

I guess there is a nasty way to figure this out by loading a html/css/js resource that requires authentication. But i'd like to do this in a "clear" way.

thx and best, Viktor

like image 846
Viktor Avatar asked Nov 05 '22 12:11

Viktor


2 Answers

there has been some discussions on that within the chromium-extensions mailing list: http://groups.google.com/a/chromium.org/group/chromium-extensions/browse_thread/thread/6e46a3a6e46d9110/

Basically, as a user suggested, you send an Xml Http Request to google.com, and search through regex, if a current user is signed in:

Source: Guillaume Boudreau (on chromium-extensions mailing list)

var currentUser;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(data) {
       if (xhr.readyState == 4) {
               currentUser = null;
               if (xhr.status == 200) {
                       var re = new RegExp(/<b class="?gb4"?>[\s]*([^<]+@[^<]+)<\/b>/i);
                       var m = re.exec(xhr.responseText);
                       if (m && m.length == 2) {
                               currentUser = m[1];
                       }
               }
               console.log("Currently logged user (on google.com): " +
currentUser);
       }
};
xhr.open('GET', 'https://www.google.com/', false);
xhr.send();
like image 185
Mohamed Mansour Avatar answered Nov 12 '22 10:11

Mohamed Mansour


chrome.identity.getAuthToken({interactive: false}, function (token) {
    if (!token) {
        if (chrome.runtime.lastError.message.match(/not signed in/)) {
            console.log("not singed in");
        } else {
            console.log("singed in");
        }
    }
});

And don't forget to add "identity" to permissions.

like image 30
holden321 Avatar answered Nov 12 '22 11:11

holden321