hapijs / yar

A hapi session manager
Other
133 stars 59 forks source link

Using Yar across different domains, or from an Ionic application #105

Closed stellasoft-will closed 8 years ago

stellasoft-will commented 8 years ago

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?

mark-bradshaw commented 8 years ago

Are you not getting the session cookies? Make sure you whitelist the api host in your ionic/cordova config.

stellasoft-will commented 8 years ago

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?

mark-bradshaw commented 8 years ago

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.

stellasoft-will commented 8 years ago

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?

mark-bradshaw commented 8 years ago

Enough to see what you're doing. The less the better.

stellasoft-will commented 8 years ago

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});
}
stellasoft-will commented 8 years ago

//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);
  });
stellasoft-will commented 8 years ago

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

mark-bradshaw commented 8 years ago

Thanks for the follow up.

lock[bot] commented 4 years ago

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.