Commit cd279a7b authored by Viacheslav Pavlov's avatar Viacheslav Pavlov Committed by Matija Obreza
Browse files

Less use of proxy

parent 2c2f45d8
......@@ -50,7 +50,7 @@ const prerenderer = (html, errHtml) => (req, res) => {
res.header('Access-Control-Allow-Headers', 'X-Requested-With');
const store = createStore(rootReducer, {} as any, applyMiddleware(thunk, routerMiddleware(createMemoryHistory)));
// Only send public data to client
store.dispatch(configure({ frontendPath: config.frontendPath, apiUrl: config.apiUrl, googleClientId: config.googleClientId, clientId: config.clientId }));
store.dispatch(configure({ frontendPath: config.frontendPath, apiUrl: config.apiUrl, googleClientId: config.googleClientId, clientId: config.clientId, anonToken: config.access_token }));
initFromServerStorage(serverStorage, store.dispatch);
console.log(`Processing request`, req._parsedOriginalUrl);
......
......@@ -7,7 +7,7 @@ import ApiCall from 'model/ApiCall';
// utility
import { log } from 'utilities/debug';
export const createApiCaller = (method, payloadType: string) => {
export const createApiCaller = (method, payloadType: string, config: {direct?: boolean, withoutTimeout?: boolean} = {direct: false, withoutTimeout: false}) => {
let cancelToken;
......@@ -24,7 +24,27 @@ export const createApiCaller = (method, payloadType: string) => {
dispatch({ type: payloadType, payload: { apiCall: ApiCall.start() } }); // Loading
return method(...ensureParamsAmount(method, params), {cancelToken: cancelToken && cancelToken.token})
const accessToken = getState().applicationConfig.anonToken.access_token;
const xhrConfig: any = {
cancelToken: cancelToken && cancelToken.token,
headers: {
common: {
Authorization: `Bearer ${accessToken}`,
},
},
};
if (config.direct) {
xhrConfig.baseURL = getState().applicationConfig.apiUrl;
}
if (config.direct) {
xhrConfig.timeout = 0;
}
return method(...ensureParamsAmount(method, params), xhrConfig)
.then((data) => { // Success
// log('Data received: ', data);
dispatch({ type: payloadType, payload: { apiCall: ApiCall.success(data) } });
......@@ -45,11 +65,11 @@ export const createApiCaller = (method, payloadType: string) => {
};
export const createPureApiCaller = (method) => {
export const createPureApiCaller = (method, config: {direct?: boolean, withoutTimeout?: boolean} = {direct: false, withoutTimeout: false}) => {
let cancelToken;
return (...params) => (dispatch) => {
return (...params) => (dispatch, getState) => {
// log('Pure Api call started');
......@@ -60,7 +80,27 @@ export const createPureApiCaller = (method) => {
cancelToken = axios.CancelToken.source();
return method(...ensureParamsAmount(method, params), {cancelToken: cancelToken && cancelToken.token})
const accessToken = getState().applicationConfig.anonToken.access_token;
const xhrConfig: any = {
cancelToken: cancelToken && cancelToken.token,
headers: {
common: {
Authorization: `Bearer ${accessToken}`,
},
},
};
if (config.direct) {
xhrConfig.baseURL = getState().applicationConfig.apiUrl;
}
if (config.direct) {
xhrConfig.timeout = 0;
}
return method(...ensureParamsAmount(method, params), xhrConfig)
.then((data) => { // Success
// log('Data received: ', data);
return Promise.resolve(data);
......
......@@ -52,12 +52,12 @@ function loginApp(d) {
// console.log('Login app', d);
configureBackendApi({ accessToken: d.access_token });
return (dispatch, getState) => {
return {
return dispatch({
type: Constants.LOGIN_APP,
authorities: [ROLE_CLIENT],
access_token: d.access_token,
...jwt.decode(d.access_token),
};
});
};
}
......
import update from 'immutability-helper';
import {CONFIGURE_APPLICATION, SET_LANG} from 'constants/applicationConfig';
import { LOGIN_APP, LOGIN_USER } from 'constants/login';
const INITIAL_STATE: {
......@@ -33,6 +34,20 @@ export default function configure(state = INITIAL_STATE, action: { type?: string
lang: {$set: action.payload},
});
}
case LOGIN_USER: {
const access_token = {...action};
delete access_token.type;
return update(state, {
anonToken: {$set: access_token},
});
}
case LOGIN_APP: {
const access_token = {...action};
delete access_token.type;
return update(state, {
anonToken: {$set: access_token},
});
}
default:
return state;
......
......@@ -11,7 +11,6 @@ import {LoginService} from 'service/LoginService';
import {UserService} from 'service/UserService';
// Util
import {clearCookies, saveCookies} from 'utilities';
import { configureBackendApi } from 'utilities/requestUtils';
// Wrapped API Calls
const apiRegister = createPureApiCaller(UserService.register);
......@@ -31,7 +30,7 @@ export const registrationRequest = (email, pass, fullName, captcha) => (dispatch
// Login
export const loginUser = (payload) => {
// console.log('loginUser', payload);
configureBackendApi({ accessToken: payload.access_token });
// configureBackendApi({ accessToken: payload.access_token }); // TODO do not configure backend depending on users token
return {
type: Constants.LOGIN_USER,
...payload,
......
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