Backbone.js + require.js + user authentication

Learning backbone.js and require.js started.

Not sure how to structure files for a web application with user authentication.

It seems like it should flow like this:

  • In the init application, a request server to check the status of an authentication session;
    • Q # 1: where should I write this code β€œafter init” of the session - in /js/app.js?
    • Q # 2: should I use jQuery ajax for this or are there any better backbone.js methods (I saw links to get (), fetch (), toJSON () in the examples)?
  • If successful, save auth data in the model (user_id, username, auth_token).
    • Q # 3: how / where can I create this model so that I can access this data in all modules? i.e. I will have a view to display a template for 'isLoggedIn.html' that will read "Hello% username%! Logout". I want to access the username field from this model. Currently, I only see how to create a new model by specifying it in the define [] view, so I don’t know how to access the model created during init.
  • Will use jQuery $ .cookies to save and retrieve auth data, so if a user leaves the page and returns, I can ask the server to check the session, and not to re-login.
    • Q # 4: how to include the jquery.cookies.js plugin in this requirejs application to use $ .cookies later, as usual? Should I add this plugin to the define [] list? Should I add it to the /js/jquery/loader.js file?

Thank you for your help.

Edit: I used files from modular-backbone to create my web application. Therefore, when I talk about /js/app.js and js / router.js, the files I refer to.

+7
source share
1 answer

I am also in this situation. I found this post and it seems that this is the best option to do something before each request will use this solution .

Before access and a URL other than / login, I am going to authenticate with a cookie or start browsing in login mode.

About how to enable other folders (jQuery cookie) - just use the require.js mechanism:

  • In your main file 'require.config' -> 'paths' add the location of the plugin (jqueryCokkie :)
  • In your view, under the 'define' section, add the path name ('jqueryCookie') and pass it to the final
  • The .js file should be in the following structure (I tried to paste the sample code here, but got problems ...).
+2
source

All Articles