reactjs - How to make initial authenticated request with token from local storage? -
i'm using vanilla flux utils
communicating apis.
on initial page load i'd read token local storage , make request api data.
i've got localstorageutils.js
library interacting window.localstorage
. container component handles login/logout actions , reads current user on page load.
app.js
componentwillmount() { localstorageutils.get('user'); }
localstorageutils
reads value , brings flux via serveraction
similar flux chat example.
get(key) { var value = window.localstorage.getitem(key); if (value) { serveractioncreators.receivefromlocalstorage(key, value); } }
that puts user userstore
, views can show username , logout link, etc.
i have apiutils.js
requesting data server. question is: tell apiutils
have logged-in user @ initial page load?
i call method inside apiutils
localstorageutils
not feel right. or have make round trip whenever change event inside container component?
you should pass user data apiutils
class , removing need being concerned how apiutils
used.
var apiutils = function () { this.get = function (endpoint, data) { return thewayyousendajaxrequests.get(endpoint).setdata(data); }; }; // wherever apiutils used. var api = new apiutils(); api.get('/me', {user: userfromstore});
Comments
Post a Comment