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) { function checkAuthTokenRequest(req, dispatch) {
const token = req.cookies.access_token; const token = req.cookies.access_token;
if (token) { if (token) {
console.log('Checking cookie token', token);
return dispatch(checkTokenRequest(token)) return dispatch(checkTokenRequest(token))
.then((checkedToken) => {
console.log(`User token ${token} is valid, response`, checkedToken);
return dispatch(loginUser(checkedToken));
})
.catch(() => { .catch(() => {
console.log('Server: check failed dispatching loginAppRequest'); console.log('Server: check failed dispatching loginAppRequest');
return dispatch(loginAppRequest()); return dispatch(loginAppRequest());
}); });
} else { } else {
console.log('Server: No token, dispatching loginAppRequest'); console.log('Server: No token in cookie, dispatching loginAppRequest');
return dispatch(loginAppRequest()); return dispatch(loginAppRequest());
} }
} }
...@@ -18,12 +23,15 @@ export default function checkAuthToken(req, res, dispatch) { ...@@ -18,12 +23,15 @@ export default function checkAuthToken(req, res, dispatch) {
console.log('Init checkAuthToken method'); console.log('Init checkAuthToken method');
return checkAuthTokenRequest(req, dispatch) return checkAuthTokenRequest(req, dispatch)
.then((data) => { .then((data) => {
console.log('Setting cookie', data.access_token);
res.cookie('access_token', data.access_token, { path: '/' }); res.cookie('access_token', data.access_token, { path: '/' });
if (data.authorities) { if (data.authorities) {
res.cookie('authorities', JSON.stringify(data.authorities), { path: '/' }); res.cookie('authorities', JSON.stringify(data.authorities), { path: '/' });
} else { } else {
res.clearCookie('authorities'); res.clearCookie('authorities');
} }
return data; return data;
}); }).catch((x) => {
console.log('Failed checkAuthToken', x);
});
} }
...@@ -11,9 +11,11 @@ export default function fetchComponentData(dispatch, branch, search) { ...@@ -11,9 +11,11 @@ export default function fetchComponentData(dispatch, branch, search) {
// Provide params to static needs // Provide params to static needs
component.needs.map((need) => { component.needs.map((need) => {
return dispatch(need({ search, params: match.params })); return dispatch(need({ search, params: match.params }));
}) }).forEach((promise) => {
// a nice array of Promises console.log(`Component ${component} needs`, promise);
.forEach((promise) => promisesWithProps.push(promise)); // a nice array of Promises
promisesWithProps.push(promise)
});
}); });
return Promise.all(promisesWithProps); return Promise.all(promisesWithProps);
......
...@@ -111,7 +111,9 @@ const prerenderer = (html) => (req, res) => { ...@@ -111,7 +111,9 @@ const prerenderer = (html) => (req, res) => {
const branch = matchRoutes(routes, pathWithoutLang); const branch = matchRoutes(routes, pathWithoutLang);
fetchComponentData(store.dispatch, branch, search) fetchComponentData(store.dispatch, branch, search)
.then(() => { return renderView(); .then(() => {
console.log('Fetched all component data');
return renderView();
}).then((html) => { }).then((html) => {
const serverRenderTime = `${Date.now() - startTime}ms`; const serverRenderTime = `${Date.now() - startTime}ms`;
console.log('Server render time:', startTime, Date.now(), serverRenderTime); console.log('Server render time:', startTime, Date.now(), serverRenderTime);
......
...@@ -52,7 +52,7 @@ class PartnerListPage extends React.Component<IBrowsePageProps, any> { ...@@ -52,7 +52,7 @@ class PartnerListPage extends React.Component<IBrowsePageProps, any> {
public componentWillMount() { public componentWillMount() {
const {pagination, paged, loadPartners} = this.props; const {pagination, paged, loadPartners} = this.props;
log(`BrowsePage.componentWillMount...`, this.props); // log(`BrowsePage.componentWillMount...`, this.props);
if (! paged || paged.filterCode !== pagination.filterCode) { if (! paged || paged.filterCode !== pagination.filterCode) {
log('Loading partners lists'); log('Loading partners lists');
......
...@@ -41,13 +41,25 @@ class PartnerPage extends React.Component<IPartnerPageProps, any> { ...@@ -41,13 +41,25 @@ class PartnerPage extends React.Component<IPartnerPageProps, any> {
]; ];
public componentWillMount() { 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); 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) => { private onEditPartner = (e) => {
const {partner, editPartner} = this.props; const {partner, editPartner} = this.props;
editPartner(partner.uuid); 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