aretherecookies-mobile/js/enhancers/authEnhancers.js
2020-03-29 19:47:33 +00:00

27 lines
718 B
JavaScript

import mapPropsStream from 'recompose/mapPropsStream';
import { compose, lifecycle } from 'recompose';
import AuthManager from '../AuthManager';
import { withRouterContext } from './routeEnhancers';
import { getPathname, loginWithBackto } from '../helpers/RouteHelpers';
export const withAuthed = mapPropsStream(props$ =>
props$.combineLatest(AuthManager.checkIsAuthed(), (props, isAuthed) => {
return {
...props,
isAuthed,
};
})
);
export const withAuthRedirect = compose(
withRouterContext,
withAuthed,
lifecycle({
componentDidMount() {
if (!this.props.isAuthed) {
const pathname = getPathname(this.props);
this.props.router.history.replace(loginWithBackto(pathname));
}
},
})
);