I’d like to update an authorization in meteor app using react- router v4 instead of v3.
const unauthenticatedPages = ['/', '/signup'];
const authenticatedPages = ['/links'];
const onEnterPublicPage = () => {
if (Meteor.userId()) {
browserHistory.replace('/links');
}
};
const onEnterPrivatePage = () => {
if (!Meteor.userId()) {
browserHistory.replace('/');
}
}
const routes = (
<Router history={browserHistory}>
<Route path="/" component={Login} onEnter={onEnterPublicPage}/>
<Route path="/signup" component={Signup} onEnter={onEnterPublicPage}/>
<Route path="/links" component={Link} onEnter={onEnterPrivatePage}/>
<Route path="*" component={NotFound}/>
</Router>
);
Tracker.autorun(() => {
const isAuthenticated = !!Meteor.userId();
const pathname = browserHistory.getCurrentLocation().pathname;
const isUnauthenticatedPage = unauthenticatedPages.includes(pathname);
const isAuthenticatedPage = authenticatedPages.includes(pathname);
if (isUnauthenticatedPage && isAuthenticated) {
browserHistory.replace('/links');
} else if (isAuthenticatedPage && !isAuthenticated) {
browserHistory.replace('/');
}
});
I’ve seen example from official react-router documentation page (https://reacttraining.com/react-router/web/example/auth-workflow). But still not sure how to do it.