Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use withCredential with $resource

I want to use resource with a cookie set in the navigator.

With $http it is really easy, as I only need to set withCredential to true:

$http({
    method: 'POST',
    url: url,
    data: user,
    withCredentials: true
});

But for $resource, I didn't find a solution to dot the same... I saw a discussion on github about that but I think that settings withCredential to true for all requests is not okay. Do you have an idea about how to do it?

like image 859
Alex Grs Avatar asked Sep 01 '13 19:09

Alex Grs


3 Answers

To change the default settings of $http (hence $resource), you need to alter $httpProvider.

Set withCredentials globally like that:

angular.module('YOUR_APP')
    .config(function($httpProvider) {
        $httpProvider.defaults.withCredentials = true;
    });
like image 124
Muhammad Reda Avatar answered Nov 15 '22 21:11

Muhammad Reda


The config withCredentials in $resource module is available in AngularJS 1.1.2+, you can get the new version and give it a try.

like image 41
zs2020 Avatar answered Nov 15 '22 19:11

zs2020


You can set it the flag globally with:

 $http.defaults.withCredentials = true;

This will affect all requests from the $resource module as well as the $http module.

like image 38
srlm Avatar answered Nov 15 '22 19:11

srlm