Closed stellasoft-will closed 8 years ago
Are you not getting the session cookies? Make sure you whitelist the api host in your ionic/cordova config.
i get session cookie under the domain the api is on, how do i get it to set that cookie under the domain angular 2 is running on? because if i run the api on localhost:9600, it does not get set on localhost:4200 when setting cookie on an api call. it works on production a the cookie lives on same domain and port, but how do i set it up to work under development environment?
Port shouldn't matter for localhost cookies. A cookie set on a Hapi server on localhost:9600 is still good for localhost:4200. Perhaps I'm misunderstanding the issue, or perhaps cordova is making additional distinctions and not passing along cookies set on different ports. To troubleshoot further I'd need some example code that I can see.
nope thats correct, the port is what seems to be affecting it. Im doing a http.post on localhost:4200 to localhost:9600 to set some cookie data, but when i make another call to the api to get the cookie data, it is undefined. i then made sure it was not the code by getting the cookie data after setting and it is there, how much code do you need?
Enough to see what you're doing. The less the better.
var yarOptions = { name:'session', maxCookieSize: 1024,// force server-side storage cache: { cache: 'session' }, cookieOptions: { password: 'some password', // cookie password isSecure: false // allow non HTTPS } };
//route code bits handler: function (request, reply) {
var basket = request.yar.get('basket');
console.log(request.yar);
if (!basket) basket = [];
var already_in_basket = false;
var payload = request.payload;
for (var i=0; i<basket.length; i++) {
var item = basket[i];
if (item.campaign == payload.campaign
&& item.product == payload.product
&& item.colour == payload.colour
&& item.size == payload.size) {
item.quantity += payload.quantity;
already_in_basket = true;
}
basket[i] = item;
}
if (!already_in_basket) {
basket.push(payload);
}
request.yar.set('basket', basket);
var basket = request.yar.get('basket');
console.log(basket);
reply({success: true});
}
//angular code var headers = new Headers(); headers.append('Content-Type', 'application/json');
return this.http.post(environment.webservice.url_base + 'user/add_to_cart', item, {headers: headers})
.map( (responseData) => {
return responseData.json();
}).subscribe(res => {
console.log(res);
}, err => {
console.log(err);
});
This was due to the following
need:
headers.append('Access-Control-Allow-Credentials', 'true');
withCredentials: true on the post options
cors:{ credentials:true }
on hapi route
Thanks for the follow up.
This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.
How can I make this work on ionic app or across domains when using this on an api. For example localhost:4200 on ionic cli to another domain or Ionic app to api?