angular icon indicating copy to clipboard operation
angular copied to clipboard

feat(http): add support for withCredentials

Open martinsik opened this issue 11 years ago • 33 comments

Hey, I'm trying to figure out how to send cookies throught CORS but since I can't access XMLHttpRequest objects directly I'm not able to set withCredentials option. I remember using it with $http in Angular 1.4. Maybe I could do this by writting custom XHRBackend? Or is there any easier way?

martinsik avatar Sep 17 '15 08:09 martinsik

@martinsik there are currently credentials properties, but they are not used anywhere yet. In fact, there's a pending PR to remove those properties until they are actually properly implemented: https://github.com/angular/angular/pull/3823

0x-r4bbit avatar Sep 17 '15 08:09 0x-r4bbit

@PascalPrecht This explains why setting mode = RequestModesOpts.NoCors or credentials = RequestCredentialsOpts.Include had no effect at all :).

So, the only thing I can do now is to wait until the Fetch spec is properly defined and properties are re-implemented?

martinsik avatar Sep 17 '15 09:09 martinsik

Yes I'm afraid that's the case. Maybe @jeffbcross has some thoughts on this.

0x-r4bbit avatar Sep 17 '15 12:09 0x-r4bbit

We're not actually using fetch at all, though in general we want the Http implementation to be fetch-like in its interfaces (of course, using Observables over Promises)

Once I finish RxNext porting (see: https://github.com/angular/angular/issues/4149), hopefully in the next couple of days, and then this sort of thing will be much easier to work with and improve on.

robwormald avatar Sep 17 '15 15:09 robwormald

It is a bit unclear to me from this thread - is withCredentials going to be supported or not please?

vladimir-ivanov avatar Nov 24 '15 10:11 vladimir-ivanov

@vladimir-ivanov me too, right now, and in the future, what can we do to set withCredentials?. At the moment, I should work with other JS libraries for the http requests, and it's not the best way to make it.

danipl avatar Nov 28 '15 10:11 danipl

Yay, mr. @robwormald has something in the workings ~ https://github.com/angular/angular/pull/5501

0x-r4bbit avatar Nov 28 '15 10:11 0x-r4bbit

i aim to please ;-)

robwormald avatar Nov 28 '15 11:11 robwormald

Pleased succesfully done. Thank you! :)

danipl avatar Nov 28 '15 11:11 danipl

If this has been fixed, was it included with the Angular2 2.0.0-beta.0? If so, how do I turn credentials on? I see some comments in #5501 but cannot figure out how to set it and not knowing if it is the current release is a concern.

brianaprince avatar Dec 30 '15 20:12 brianaprince

@brianaprince, here is a workaround I've been using for now...

@Injectable()
export class Auth {
  isAuthenticated = false;
  user: User;

  constructor(@Inject(Http) private http: Http) {
    // TODO: Use official Angular2 CORS support when merged (https://github.com/angular/angular/issues/4231).
    let _build = (<any> http)._backend._browserXHR.build;
    (<any> http)._backend._browserXHR.build = () => {
      let _xhr =  _build();
      _xhr.withCredentials = true;
      return _xhr;
    };
  }
...

icereval avatar Jan 02 '16 22:01 icereval

icereval, thanks for the workaround.

brianaprince avatar Jan 04 '16 12:01 brianaprince

@brianaprince I don't know if it would help but I've modified BaseRequestOptions to handle withCredentials. Forked repository is here, and PR is https://github.com/angular/angular/pull/6236. It would be helpful till this feature is officially supported.

class MyBaseRequestOptions extends BaseRequestOptions {
  headers: Headers = new Headers({
    'X-Requested-With': 'XMLHttpRequest'
  });
  withCredentials: boolean = true;
}

bootstrap( AppComponent, [ provide(RequestOptions, {useClass: MyBaseRequestOptions}) ] );

yusugomori avatar Jan 05 '16 00:01 yusugomori

+1

gustavolira avatar Jan 15 '16 18:01 gustavolira

@icereval Thank you for you contribution, your workaround is working for me! Please can you tell me how to add a custom header at that workaround? Thank you

gustavolira avatar Jan 19 '16 12:01 gustavolira

This is getting a bit off topic but I'll try my best to answer w/ a clear example, @gustavolira you should be able to use the builtin Angular2 Headers class: https://angular.io/docs/ts/latest/api/http/Headers-class.html

When performing a CORS request you'll need to ensure your preflight OPTIONS response contains the Access-Control-Allow-Headers: X-Api-Key header w/ your custom header name(s).

e.g.

import {Http, Headers} from 'angular2/http';
...
private fetchCurrentUser(): Promise<User> {
  return new Promise<User>((resolve) => {
    let headers = new Headers();
    headers.append('X-Api-Key', 'Foo');
    return this.http.get(`${Settings.API_BASE}users/me`, {headers: headers})
      .map(res => res.json())
      .subscribe(resolve);
  });
}

icereval avatar Jan 20 '16 00:01 icereval

note that you can just do someObservable.toPromise() rather than that wrapping business.... you just need to import the toPromise operator:

import 'rxjs/add/operator/toPromise'

someObservable.toPromise()

robwormald avatar Jan 20 '16 04:01 robwormald

Is there a reason why this hasn't made it to master? I see that this is still open and #5501 from @robwormald was merged but there's no code included. Seems strange to me, but I'm not a core committer. Would be nice to have this.

smithrobs avatar Jan 26 '16 18:01 smithrobs

The patch from @icereval works for me, very ugly though. This is sort of a must for CORS.

tlong-dev avatar Jan 29 '16 15:01 tlong-dev

Would also love to have this for CORS support.

sumigoma avatar Feb 04 '16 19:02 sumigoma