2016-05-20 01:11:43 +05:30
|
|
|
import { authenticate, changeLang } from 'components/user/actions';
|
2016-05-10 10:47:40 +05:30
|
|
|
|
2016-08-08 00:47:58 +05:30
|
|
|
import request from 'services/request';
|
|
|
|
import bearerHeaderMiddleware from './middlewares/bearerHeaderMiddleware';
|
|
|
|
import refreshTokenMiddleware from './middlewares/refreshTokenMiddleware';
|
|
|
|
|
2016-05-10 10:47:40 +05:30
|
|
|
/**
|
|
|
|
* Initializes User state with the fresh data
|
|
|
|
*
|
2016-07-31 19:23:16 +05:30
|
|
|
* @param {object} store - redux store
|
2016-05-10 10:47:40 +05:30
|
|
|
*
|
2016-08-08 00:47:58 +05:30
|
|
|
* @return {Promise} - a promise, that resolves in User state
|
2016-05-10 10:47:40 +05:30
|
|
|
*/
|
|
|
|
export function factory(store) {
|
2016-08-08 00:47:58 +05:30
|
|
|
request.addMiddleware(refreshTokenMiddleware(store));
|
|
|
|
request.addMiddleware(bearerHeaderMiddleware(store));
|
|
|
|
|
2016-05-10 10:47:40 +05:30
|
|
|
return new Promise((resolve, reject) => {
|
2016-05-20 01:11:43 +05:30
|
|
|
const {user} = store.getState();
|
|
|
|
|
|
|
|
if (user.token) {
|
2016-05-10 10:47:40 +05:30
|
|
|
// 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-10 10:47:40 +05:30
|
|
|
}
|
2016-05-20 01:11:43 +05:30
|
|
|
|
|
|
|
// auto-detect guests language
|
2016-05-29 00:53:34 +05:30
|
|
|
store.dispatch(changeLang(user.lang)).then(resolve, reject);
|
2016-05-10 10:47:40 +05:30
|
|
|
});
|
|
|
|
}
|