How use token authentication with Rails, Devise and Backbone.js?

后端 未结 3 1082
旧时难觅i
旧时难觅i 2020-12-07 08:10

I\'m trying to build a mobile application with PhoneGap, jQuery Mobile and Backbone.js on the client-side - with a Rails 3 JSON API running server-side.

I know how t

3条回答
  •  温柔的废话
    2020-12-07 09:06

    Why don't append it to all of your jquery ajax requests. It will add the auth_token to all of your ajax calls over jQuery. That might be useful when working directly with jQuery ajax (or libs that do so). But this might be a security issue as well (when you have ajax calls to other sites...).

    // this is untested
    $.ajaxSetup({ beforeSend : function(xhr, settings){ 
    
      // just because the auth_token is a private information
      if(!settings.crossDomain) {
    
        // parse data object
        var dataobj = JSON.parse(xhr.data);
    
        // add authentication token to the data object
        dataobj.auth_token = AUTHENTICATION_TOKEN;
    
        // save the dataobject into the jqXHR object
        xhr.data = JSON.stringify(dataobj); 
    
      }
    }});
    

    Another approach may be to write that token into the header and process it on the server side:

    // thats not beautiful
    $.ajaxSetup({ headers : { "auth_token" : AUTHENTICATION_TOKEN } });
    

提交回复
热议问题