Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular2 xhrfields withcredentials true

I am trying to login to a system. In angular 1, there was ways to set

withCredentials:true

But I could not find a working solution in angular2

export class LoginComponent {
    constructor(public _router: Router, public http: Http, ) {

    }


    onSubmit(event,username,password) {
        this.creds = {'Email': '[email protected]','Password': '01010','RememberMe': true}
        this.headers = new Headers();
        this.headers.append('Content-Type', 'application/json');

        this.http.post('http://xyz/api/Users/Login', {}, this.creds)
              .subscribe(res => {
                  console.log(res.json().results);

              });
     }

}
like image 769
harikrish Avatar asked Jan 26 '16 05:01

harikrish


3 Answers

In Angular > 2.0.0 (and actually from RC2 on), just

http.get('http://my.domain.com/request', { withCredentials: true })
like image 157
maxbellec Avatar answered Nov 10 '22 23:11

maxbellec


AFAIK, right now (beta.1) the option is not available.

You have to work around it with something like this:

let _build = http._backend._browserXHR.build;

http._backend._browserXHR.build = () => {
  let _xhr =  _build();
  _xhr.withCredentials = true;
  return _xhr;
};
like image 5
cexbrayat Avatar answered Nov 10 '22 22:11

cexbrayat


This issue has been noted by the angular2 team.

You can find some other workarounds (one especially written as an @Injectable) following the issue link.

like image 2
grebett Avatar answered Nov 11 '22 00:11

grebett