Jer_TX
Jer_TX

Reputation: 474

Angular hybrid app doesn't appear to be using the same session as angularjs

I have a hybrid angular/angularJS app that seems to work well as basically just a wrapper for angularjs. We're not currently using any angular components or services, all logic still exists in angularjs.

As a step forward to migrating into angular, I'm trying to write a new component to display some data, but I'm finding that when I make http requests, it doesn't seem to be using the same session that exists under angularjs. The exact same http request under angularJS works just fine, but under Angular, I'm no longer authenticated. Login/authentication happens under the AngularJS logic but since both angularjs and angular are effectively the same "app" once compiled, I'm not sure how this isn't working.

Why is this, and how can I fix it? I have no idea what information/code is helpful to see in this scenario as I'm not entirely sure what's even wrong here.

This is in my constructor for the Angular component:

private _listHealthDataUrl: string = "http://localhost:6543/rpc/json/health/get_health_entries";

getData(): Observable<IHealthResponse> {
    return this.http.post<IHealthResponse>(this._listHealthDataUrl, {}, {})
}

And the exact same request done from within angularJS:

$http({
    url: 'http://localhost:6543/rpc/json/health/get_health_entries',
    method: 'POST',
    data: {}
    }
}).then(function (e) {...})

The request headers, as far as what Chrome tells me, are exactly the same:

Provisional headers are shown
Accept: application/json, text/plain, */*
Content-Type: application/json;charset=UTF-8
Origin: http://localhost:8080
Referer: http://localhost:8080/health
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36

The request headers as for what the server seems to read:

{'Origin': 'http://localhost:8080', 'Content-Length': '2',
 'Cache-Control': 'no-cache',
 'Accept': 'application/json, text/plain, */*',
 'Content_Length': '2',
 'Referer': 'http://localhost:8080/bloodpressure',
 'Content-Type': 'application/json',
 'X-Appengine-Country': 'ZZ',
 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36',
 'Pragma': 'no-cache', 
 'Accept-Language': 'en-US,en;q=0.9', 
 'Host': 'localhost:6543', 
 'Content_Type': 'application/json'}

Again, exactly the same except on the AngularJS, the headers include a session header. I'm not sure where that comes from on AngularJS side of things or why Angular doesn't have include it.

Upvotes: 0

Views: 622

Answers (1)

Jer_TX
Jer_TX

Reputation: 474

The problem was $http's default options withCredentials. I was mistaken in that it wasn't a session header, but a cookie with the name session. massive facepalm.

In angularJS, you can simply do

$http.defaults.withCredentials = true;

from within .run() but seems angular doesn't have such a thing. Fix there is using an interceptor on HttpClient, or otherwise including withCredentials on the options section of every http request.

Upvotes: 1

Related Questions