Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement user authentication in a Chrome Extension using Google App Engine as backend?

This is a follow up to my previous question.

I am working on a Chrome extension http://ting-1.appspot.com/ that saves the bookmarked page to Google App Engine backend. Looking at the Chrome web store I see that extensions have an "add to chrome" button. Since my extension requires communication with the backend (so the user must have a gmail account to use this extension) how do I indicate in the extension to use the username (the gmail address of the person who added the extension to Chrome) to write the bookmark to google app engine with his user id? I have a gap in my understanding and I don't seem to find anything related to this question in the documentation. My background.html is below. Thanks.

background.html

<html>
<script>
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.getSelected(null, function(tab) {

  // Send a request to the content script.
  chrome.tabs.sendRequest(tab.id, {action: "getDOM"}, function(response) {
    var firstParagraph = response.dom;
    console.log("background -- before *console.log(response.dom)*")
    console.log("this is *console.log(response.dom)*: " + response.dom)
    console.log("background -- after *console.log(response.dom)*")
  //}); moved to end to get the variable firstParagraph

var formData = new FormData();
formData.append("url", tab.url);
formData.append("title", tab.title);
formData.append("pitch", firstParagraph);

var xhr = new XMLHttpRequest();
xhr.open("POST", "http://ting-1.appspot.com/submithandlertest", true);
xhr.onreadystatechange = function (aEvt) {
    if (xhr.readyState == 4) {
        if (xhr.status == 200){ 
            console.log("request 200-OK");
            chrome.browserAction.setBadgeText ( { text: "done" } );
            setTimeout(function () {
            chrome.browserAction.setBadgeText( { text: "" } );
            }, 2000);
        }else{
            console.log("connection error");
            chrome.browserAction.setBadgeText ( { text: "ERR" } );
     }        
  }        
};
xhr.send(formData);
}); //chrome.tabs.sendRequest
        });
    });
</script>
</html>
like image 212
Zeynel Avatar asked Oct 20 '11 01:10

Zeynel


1 Answers

Your extension needs to send the user to your app to sign in, so that the appropriate cookies will be set and your extension can authenticate as the user. The Chrome to Phone extension does this, and you can examine its source to see how.

like image 66
Nick Johnson Avatar answered Oct 04 '22 21:10

Nick Johnson