accounts-frontend/src/components/user/factory.js

30 lines
947 B
JavaScript
Raw Normal View History

2016-05-20 01:11:43 +05:30
import { authenticate, changeLang } from 'components/user/actions';
import request from 'services/request';
import bearerHeaderMiddleware from './middlewares/bearerHeaderMiddleware';
import refreshTokenMiddleware from './middlewares/refreshTokenMiddleware';
/**
* Initializes User state with the fresh data
*
* @param {object} store - redux store
*
* @return {Promise} - a promise, that resolves in User state
*/
export function factory(store) {
request.addMiddleware(refreshTokenMiddleware(store));
request.addMiddleware(bearerHeaderMiddleware(store));
return new Promise((resolve, reject) => {
2016-05-20 01:11:43 +05:30
const {user} = store.getState();
if (user.token) {
// authorizing user if it is possible
2016-05-20 01:11:43 +05:30
return store.dispatch(authenticate(user.token)).then(resolve, reject);
}
2016-05-20 01:11:43 +05:30
// auto-detect guests language
store.dispatch(changeLang(user.lang)).then(resolve, reject);
});
}