Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Based on what you've described, storing the <code>auth_token</code> in browser's session cookie seems to be the way to go.</p> <p>Backbone.js doesn't support cookie manipulation. However, you can use a jQuery plugin/write your own cookie manipulator to handle this.</p> <p>Assuming you're using <code>jquery-cookie</code> (<a href="https://github.com/carhartl/jquery-cookie" rel="nofollow">https://github.com/carhartl/jquery-cookie</a>), here's an example on how you can achieve this (It's on their Wiki too!):</p> <pre><code>$.cookie('auth_token', authTokenValue); </code></pre> <p>For the API you interact with, depends on how they accept the <code>auth_token</code>, you may need to create a BaseModel on top of Backbone.Model to handle the use of <code>auth_token</code> automatically.</p> <p>For example, if the API expects you to pass the <code>auth_token</code> as a part of QueryString, you'll need to override <code>Backbone.Model.url()</code> function:</p> <pre><code>var BaseModel = Backbone.Model.extend({ url: function() { var base = _.result(this, 'urlRoot') || _.result(this.collection, 'url') || urlError(); if (this.isNew()) return base + '?' + $.cookie('auth_token'); return base + (base.charAt(base.length - 1) === '/' ? '' : '/') + encodeURIComponent(this.id) + '?' + $.cookie('auth_token'); } }); var EntityModel = BaseModel.extend({ ... }); </code></pre> <p>So, how you override the Backbone.Model depends on what the API endpoint expects.</p> <p>If following <a href="http://whatcodecraves.com/articles/2012/01/11/backbonejs-sessions-and-authentication" rel="nofollow">this</a> is what your ultimate goal is, there are couple things you can do:</p> <pre><code>App.Models.Session = Backbone.Model.extend defaults: access_token: null, user_id: null initialize: -&gt; @load() authenticated: -&gt; Boolean(@get("auth_token")) login: (email, password, options)-&gt; # make an AJAX call to the authentication API # once returned, call @save(...) with auth_token that you got back. # options is there to facilitate that, if you want to pass in an onAuthencated or onNotAuthenticated callbacks, you can. # Saves session information to cookie save: (auth_token)-&gt; $.cookie('auth_token', auth_token) # Loads session information from cookie load: -&gt; @set access_token: $.cookie('auth_token') App.start = -&gt; @session = new App.Models.Session() if @session.authenticated() # redirect to user page else # launch a login form # call @session.login(email, password) </code></pre> <p>As a side note, instead of using the <code>option</code> param in @login(email, password, options), you can also trigger an event, like <code>@trigger('authenticated')</code> from the model, letting the View/App knows that the user is now authenticated. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload