Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic redirect after login with react-router

I wanted to build a Facebook login into my react/react-router/flux application. I have a listener registered on the login event and would like to redirect the user to '/dashboard' if they are logged in. How can I do that? location.push didn't work very well, except after reloading the page completely.

like image 563
Daniel Schmidt Avatar asked Apr 12 '15 21:04

Daniel Schmidt


People also ask

How do I redirect after login?

To do this, go to Settings » Confirmation from the left column, then select 'Go to URL' redirect as your confirmation type. Then, you can enter the URL where your users will be redirected. Your login form is now ready. Make sure you click the 'Save' button before closing the form builder interface.


1 Answers

React Router v3

This is what I do

var Router = require('react-router'); Router.browserHistory.push('/somepath'); 

React Router v4

Now we can use the <Redirect>component in React Router v4.

Rendering a <Redirect> will navigate to a new location. The new location will override the current location in the history stack, like server-side redirects.

import React, { Component } from 'react'; import { Redirect } from 'react-router'; export default class LoginComponent extends Component {     render(){         if(this.state.isLoggedIn === true){             return (<Redirect to="/your/redirect/page" />);         }else{             return (<div>Login Please</div>);         }     } } 

Documentation https://reacttraining.com/react-router/web/api/Redirect

like image 115
Sajjad Ashraf Avatar answered Oct 08 '22 18:10

Sajjad Ashraf