React Router redirect on reaload


#1

Greetings, I am experimenting on custom admin view page and do have one question about the behavior.

The current logic at the moment is:
If the user in not authenticated and tries to visit /admin-panel or its children he/she gets redirected to /& login page.
If user in logged in and visits /& (login page) he/she gets redirected to /admin-panel
The problem is when the user is lodded in and is on /admin-panel and reloads the page the following occurs:

  1. First /& page loads
  2. Then /& redirects user to /admin-panel
    If the user was on or /admin-panel/child_component after reload he/she will be on /admin-panel and will have to navigate again to /child_component

Can you please explain what is the cause of current behavior and if there is some way to make user stay on the page the reload was initiated and can the constant redirection be avoided?

The login page /&

import React, { Component } from 'react';
import { Link, browserHistory } from 'react-router';
import { Tracker } from 'meteor/tracker'

class Backdoor extends Component {

  onSubmit(event) {
    event.preventDefault();
    
    // Collecting user input
    const self = this;
    const email = $(event.target).find('[name=email]').val();
    const password = $(event.target).find('[name=password]').val();

    Meteor.loginWithPassword(email, password, function (err) {
        browserHistory.push('admin-panel');
    });
  }

  componentWillMount(){
    Tracker.autorun(() => {
      if (Meteor.user()) {
        browserHistory.push('/admin-panel')
      } else if(!Meteor.user()) {
  			browserHistory.push('/&')
      }
    })
  }

  render() {
    return (

    // Login form
    );
  }
}

export default Backdoor;

React-router paths’:

const routes = (
  <Router history={browserHistory}>

    <Route path='/' component={App}>
      <Router path='about' component={About} />
    </Route>

    <Route path='&' component={Backdoor} />

    <Route path='admin' component={AdminPanel}>
      <Router path='/admin/admin_component' component={AdminChild} />
    </Route>

  </Router>

#2

did you get it solved??? i have the same problem…