Skip to content

Instantly share code, notes, and snippets.

@blackxored
Created July 2, 2012 04:44
Show Gist options
  • Save blackxored/3031146 to your computer and use it in GitHub Desktop.
Save blackxored/3031146 to your computer and use it in GitHub Desktop.
Token authentication with Devise and Backbone
jQuery.ajaxSetup(
beforeSend: (xhr) ->
xhr.setRequestHeader('X-Auth-Token',
App.Session.getAuthenticationToken())
)
# ...
class App.Session
# ...
getAuthenticationToken: ->
user = @getCurrentUser()
user.authenticationToken if user?
# ...
module Devise
module Strategies
class TokenAuthenticatable < Authenticatable
# TODO: Monkey-patch for allowing X-Auth-Token in headers
private
def params_auth_hash_with_headers
if request.headers['X-Auth-Token']
params.merge!({ :auth_token => request.headers['X-Auth-Token']})
end
params_auth_hash_without_headers
end
alias_method :params_auth_hash_without_headers, :params_auth_hash
alias_method :params_auth_hash, :params_auth_hash_with_headers
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment