Commit 749ddc98 authored by Matija Obreza's avatar Matija Obreza
Browse files

Fix: Backend must configure axiosBackend for user in SSR

- Fixes: Partner display page should not need to reload partner info after SSR
parent 766d34ed
import {checkTokenRequest, loginAppRequest} from 'actions/login';
import { checkTokenRequest, loginAppRequest, loginUser } from 'actions/login';
function checkAuthTokenRequest(req, dispatch) {
const token = req.cookies.access_token;
if (token) {
console.log('Checking cookie token', token);
return dispatch(checkTokenRequest(token))
.then((checkedToken) => {
console.log(`User token ${token} is valid, response`, checkedToken);
return dispatch(loginUser(checkedToken));
})
.catch(() => {
console.log('Server: check failed dispatching loginAppRequest');
return dispatch(loginAppRequest());
});
} else {
console.log('Server: No token, dispatching loginAppRequest');
console.log('Server: No token in cookie, dispatching loginAppRequest');
return dispatch(loginAppRequest());
}
}
......@@ -18,12 +23,15 @@ export default function checkAuthToken(req, res, dispatch) {
console.log('Init checkAuthToken method');
return checkAuthTokenRequest(req, dispatch)
.then((data) => {
console.log('Setting cookie', data.access_token);
res.cookie('access_token', data.access_token, { path: '/' });
if (data.authorities) {
res.cookie('authorities', JSON.stringify(data.authorities), { path: '/' });
res.cookie('authorities', JSON.stringify(data.authorities), { path: '/' });
} else {
res.clearCookie('authorities');
res.clearCookie('authorities');
}
return data;
});
}).catch((x) => {
console.log('Failed checkAuthToken', x);
});
}
......@@ -11,9 +11,11 @@ export default function fetchComponentData(dispatch, branch, search) {
// Provide params to static needs
component.needs.map((need) => {
return dispatch(need({ search, params: match.params }));
})
// a nice array of Promises
.forEach((promise) => promisesWithProps.push(promise));
}).forEach((promise) => {
console.log(`Component ${component} needs`, promise);
// a nice array of Promises
promisesWithProps.push(promise)
});
});
return Promise.all(promisesWithProps);
......
......@@ -111,7 +111,9 @@ const prerenderer = (html) => (req, res) => {
const branch = matchRoutes(routes, pathWithoutLang);
fetchComponentData(store.dispatch, branch, search)
.then(() => { return renderView();
.then(() => {
console.log('Fetched all component data');
return renderView();
}).then((html) => {
const serverRenderTime = `${Date.now() - startTime}ms`;
console.log('Server render time:', startTime, Date.now(), serverRenderTime);
......
......@@ -52,7 +52,7 @@ class PartnerListPage extends React.Component<IBrowsePageProps, any> {
public componentWillMount() {
const {pagination, paged, loadPartners} = this.props;
log(`BrowsePage.componentWillMount...`, this.props);
// log(`BrowsePage.componentWillMount...`, this.props);
if (! paged || paged.filterCode !== pagination.filterCode) {
log('Loading partners lists');
......
......@@ -41,13 +41,25 @@ class PartnerPage extends React.Component<IPartnerPageProps, any> {
];
public componentWillMount() {
const {uuid, loading, loadPartner} = this.props;
const {uuid, partner, loadPartner} = this.props;
if (uuid && (! loading || loading.uuid !== uuid)) {
if (uuid && (! partner || partner.uuid !== uuid)) {
console.log(`cwm: Loading partner ${uuid}`, partner);
loadPartner(uuid);
}
}
public componentWillReceiveProps(nextProps) {
const {uuid, partner, loading, loadPartner} = nextProps;
if (uuid && (! partner || partner.uuid !== uuid)) {
if (! loading || loading.uuid !== uuid) {
console.log(`cwrp: Loading partner ${uuid}`, partner, loading);
loadPartner(uuid);
}
}
}
private onEditPartner = (e) => {
const {partner, editPartner} = this.props;
editPartner(partner.uuid);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment