olebedev/go-starter-kit

How to pass headers to the goja VM?

iKonrad opened this issue · 1 comments

Hey. I'm trying to implement a cookie based authentication, but when gojva VM renders the page (and therefore calls several API endpoints) and it doesn't have the client cookies.

I can see in react.go, on line 69 we're passing the headers, but they're not set on the client side.

These are the headers from the client:

map[Cookie:[XDEBUG_SESSION=XDEBUG_ECLIPSE; wp-settings-time-1=1488570966; __utma=XXXXXXXXXXXXXXXXX; __utmz=111872281.1482158907.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); csrf_token=XXXXXXXXXXXXXXXX; ab_blog=MTQ5MDkxNTM3OXxEdi1CQkFFQ180SUFBUkFCRUFBQUJQLUNBQUE9fBgB9F6lZSoL3vGHIevcHuxKSC2SIEzweVtY3eOx3vuK] Cache-Control:[max-age=0] Upgrade-Insecure-Requests:[1] User-Agent:[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36] Accept:[text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8] Accept-Encoding:[gzip, deflate, sdch, br] Accept-Language:[en] Connection:[keep-alive]]

And these ones are from the VM:

2017/04/03 10:23:57 map[User-Agent:[golang-fetch/0.1 (+https://github.com/olebedev/gojax/fetch)] Connection:[close] Accept:[/]]

Alright, if anyone had the same issue, the cookies are not passed because they're not set.

We can't just set them via document.cookie because the code is rendered on the server and document is undefined.

To solve that I've saved those cookies in the global variable in toString.js:

global.clientCookies = options.headers.Cookie[0];

and then included it in the ajax call:

`
let configs = {
credentials: 'include',
};

        if (typeof(global.clientCookies) !== 'undefined') {
            configs['headers'] = {
                Cookie: global.clientCookies
            }
        }

        return fetch('/api/v1/conf', configs).then((r) => {
            return r.json();
        }).then((conf) => {
            dispatch({ type: Constants.SET_CONFIG, config: conf });
        });

`

If anyone has any suggestion how to save those cookies in the proper way, please advise. :)

Closing it for now.