javascript - Chrome extensionweb app session control - Stack Overflow

I am creating a chrome extension, rather a chrome webapp. This application just contains the html, js,

I am creating a chrome extension, rather a chrome webapp. This application just contains the html, js, image and css files. The application connects to a server to fetch data. I chose to do this as it would reduce the amount of files downloaded by the user. Using Backbone.js I have an MVC architecture in my application. Thus the application just sends json.

Now having said this, I need a session management. I plan to use Google authentication as the organization has Google Apps. I need a method that once the user has logged in using google auth the server get the user name every time the application makes a request.

Is it a good idea to add the user name in request header, if possible. Or should I use cookies? Can any one tell me how I could go about using cookies in this case?

I am creating a chrome extension, rather a chrome webapp. This application just contains the html, js, image and css files. The application connects to a server to fetch data. I chose to do this as it would reduce the amount of files downloaded by the user. Using Backbone.js I have an MVC architecture in my application. Thus the application just sends json.

Now having said this, I need a session management. I plan to use Google authentication as the organization has Google Apps. I need a method that once the user has logged in using google auth the server get the user name every time the application makes a request.

Is it a good idea to add the user name in request header, if possible. Or should I use cookies? Can any one tell me how I could go about using cookies in this case?

Share Improve this question edited Aug 9, 2011 at 14:59 serg 111k78 gold badges325 silver badges337 bronze badges asked Aug 9, 2011 at 4:01 satransatran 1,2424 gold badges17 silver badges28 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

This might be a late response but I want to present a more elegant solution to you given that the user has cookies enabled in their browser. First read my answer on another question.

Now that you can send cross origin xhr from your content scripts all you need to do is store all your authentication and session management at server only. That is right, you just need to display whether the user is logged in or not and a logout button at client based on server response.

Just follow these steps.

  1. At client Whenever user accesses your chrome web app, blindly make XmlHttpRequests to your server without worrying about authentication, just keep a tab on response from server which I describe below.

  2. At server whenever you receive a request check for valid sessions or session cookie. If session is valid send proper response, if not send error, 401 or any other response to municate to your client that session is not valid. It is better if you send an error code like 401 since then you can put a generic script at client to inform them that they are not logged in.

  3. At Client If response from server is proper, display it, else display login link to your website.

  4. IMPORTANT: Display logout button if user is logged in.

Check out my implementation of this in my extension

For help using Google authentication in your app take a look at Google's OAuth tutorial which es with all you need (took me no time to set it up using this).

As for session management. The implementation of OAuth used by Google stores the tokens in localStorage. Also, as briefly mentioned in the extensions overview we are expected to use localStorage to store data. Thus, I suggest you store the users name here as it will be accessible throughout the app's lifetime (until it is uninstalled). However, you may need to manage the name stored here and consider what should happen when users log in and out. That said; I'm not sure if sessionStorage would be a better option as I've never used it before, let alone in an extension.

Note

localStorage and its counterparts only store strings so I suggest using a wrapper which uses JSON to parse and stringify to get and set your values respectively.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745442664a4627899.html

相关推荐

  • javascript - Chrome extensionweb app session control - Stack Overflow

    I am creating a chrome extension, rather a chrome webapp. This application just contains the html, js,

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信