totaljs / framework

Node.js framework
http://www.totaljs.com
Other
4.36k stars 450 forks source link

RESTBuilder – instance.cookie('key', 'value') not working. #616

Closed sabsaxo closed 6 years ago

sabsaxo commented 6 years ago

I've been trying for a long time to set a cookie from a RESTBuilder instance (placed in a definition) using:

F.helpers.getConstants = function ( callback ) {
    let constants = {};
    let cookies = {};

    < ... >

    RESTBuilder.make(function ( builder ) {
        builder.cookies( cookies );
        builder.cook( true );

        builder.exec(function ( err, response, output ) {
            console.log("Cookies:", cookies );

            /**
             * Outputs:
             * Cookies: { csrftoken: '8Cn3at5n9oW1CgxrXmbrELIxjdpDAGYj',
             * sessionid: 'dseh0llwjhi8hk7rfix38igi7s648fy1' }
             */

            builder.cookie('csrftoken', cookies.csrftoken);
            builder.cookie('sessionid', cookies.sessionid);

            < ... >

            if ( callback ) {
                callback( constants );
            }
        });
    });
};

I'm only able to set them back in the calling controller, passing the values back from the helper. But I'd really like to be able to set this in a central place, so I don't have to place controller.cookie( ... ) all over my controllers.

Am I doing it wrong (missing the point), or is this a bug?

sabsaxo commented 6 years ago

When I console.log the instance, I can see the cookies just fine, and $persistentcookies are set to true:

RESTBuilder {
    '$url': 'http://localhost:8000/main/api/constants/',
    '$headers': 
    { 'user-agent': 'Total.js/v2.9.4',
        accept: 'application/json, text/plain, text/plain, text/xml' },
    '$method': 'get',
    '$timeout': 10000,
    '$type': 0,
    '$length': 0,
    '$transform': undefined,
    '$files': null,
    '$persistentcookies': true,
    '$cookies': 
    { csrftoken: 'xunv27vWsPgl8xp7q2E7UcKYIJiESIKk',
        sessionid: '0bhtap9p9v9esfmw55lumy2kqfi6jqdi'
    },
    '$flags': [ 'get', 'dnscache', 'cookies' ]
}
petersirka commented 6 years ago

Try this:

var cookies = {};
F.helpers.getConstants = function ( callback ) {

    let constants = {}; 

    RESTBuilder.make(function ( builder ) {
        builder.cookies( cookies );
        builder.cook( true );

        builder.exec(function ( err, response, output ) {
            console.log("Cookies:", cookies );

            /**
             * Outputs:
             * Cookies: { csrftoken: '8Cn3at5n9oW1CgxrXmbrELIxjdpDAGYj',
             * sessionid: 'dseh0llwjhi8hk7rfix38igi7s648fy1' }
             */

            if ( callback ) {
                callback( constants );
            }
        });
    });
};
petersirka commented 6 years ago

RESTBuilder.make(function ( builder ) { creates a new instance of RESTBuilder, so it's useless to set builder.cookie() after you perform .exec().

sabsaxo commented 6 years ago

Same result, no cookies set.

OK, then I totally do not understand that part of the framework. Where would I centrally place a cookie that will update on a call that gets made everytime a csrftoken is renewed?

sabsaxo commented 6 years ago

And how can I then avoid to create new instance everytime? The url doesn't change, so I guess the instance doesn't need to neither.

sabsaxo commented 6 years ago

So, what is happening here? Are the cookies reset after .exec() ... or what are you saying?

sabsaxo commented 6 years ago

I request the node server for a login form. When submitting the form I request a Django backend on another server, and I need to make cookies persist across the two servers.

I can do that when I set the cookies from the controller and adding 'withCredentials' : true to my xhr, but I'd like to avoid to have to set them in each and every controller method across the whole application ... so, what I'm after is a 'structure' for how to make this a central thing in the request chain.

petersirka commented 6 years ago

So, what is happening here? Are the cookies reset after .exec() ... or what are you saying?

No. All is about reference to cookie object. RESTBuilder binds received cookies after .exec. I wrote you about it in Total.js messenger. As I said: I help you within Total.js support.