reactjs - Redirect after login to the original url in react-router -
hi using erikras' react-redux-universal-hot-example
my problem trying visit page lets "/chat" , in doing should redirected "/login" page since needs authentication. once complete authentication should redirected original url "/chat".
i trying use onenter hook didn't work. after login redirecting users "/loginsuccess" page default (hard coded)
thanks in advance help
here's how approach high level...
- when redirect
/login
, add current route next route's location state. - handle login.
- when navigating
/loginsuccess
, check location state route passed, , redirect that.
it this...
// function passed onenter handle authentication check redirectifnotloggedin(nextstate, replace) { if (notloggedin) { replace({ pathname: '/login', state: { redirectto: nextstate.location.pathname } }) } } // function passed onenter handle loginsucesss redirect redirectafterloggedin(nextstate, replace) { if(nextstate.location.state.redirectto) { replace(nextstate.location.state.redirectto) // might need prepend '/' } }
Comments
Post a Comment