Automatically change route on login


#1

I have a /login route which currently just renders the AccountsUIWrapper component. Upon logging in (or if this route is somehow navigated to while logged in), I’d like to re-route the user the their dashboard.

I had assumed something like below would work:

const onEnterLogin = () => {
	if (Meteor.user()) {
		browserHistory.push('/dashboard')
	}
}

<Route path="login" component={Login} onEnter={onEnterLogin}/>

though upon login the user is not redirected. Using react-router, how can I re-route the user automatically once they’ve logged in?

EDIT: I assume I could do this by logging in programmatically instead of using AccountsUIWrapper and using browserHistory.push() on success. However, I’d like a way to do this using AccountsUIWrapper.


#2

Using FlowRouter:

FlowRouter.route('/login', {
  name: 'login',
  triggersEnter: [() => {
    if (Meteor.userId()) {
      FlowRouter.go('dashboard')
    }
  }],
  action (params, queryParams) {
    mount(MainLayout, {
      navbar: () => (<Navbar />),
      content: () => (<Login />),
      footer: () => (<Footer />)
    })
  }
})

#3
import { Meteor } from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';

if (Meteor.isClient) {
  Accounts.onLogin(() => browserHistory.push('/dashboard'));
}

#4

Rather than handling this at the route level, you can add the logic to a layout component