AngularJs + ngResource + REST + Rails + Devise (warden)

Today I had a hard day with angular and rails,
I attempted to make Angular communicate with Rails via REST api and Devise auth

Sounds nothing special.
But there is one little issue which cause troubles in many cases.
that is a X-Requested-With: XMLHttpRequest header
which is not set by angular link

But devise and warden logic heavily depends on request.xhr?
I had to add it at angular side.
And one more issue, to make angular save/use cookies i have added withCredentials: true
In a result angular resource looks like:

app.factory('Sessions', function($resource){
    return $resource('http://localhost:3000/api/users/sign_in', {}, {
    create: {method: 'POST', headers: {'X-Requested-With': 'XMLHttpRequest'}, withCredentials: true}
    })
})
Profile photo of tyatin

tyatin Author

Leave a Reply