Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to previous route after a login success

I am having a hell of a time trying to understand the best way to do this and where to even handle this redirect at.

I found an example of creating a ProtectedRoute component which is setup like this

const ProtectedRoute = ({ component: Component, ...rest }) => {
  return (
    <Route {...rest} render={props => (rest.authenticatedUser ? (<Component {...props}/>) : (
      <Redirect to={{
        pathname: '/login',
        state: { from: props.location }
      }}/>
    )
    )}/>
  );
};

And used like this

<ProtectedRoute path="/" component={HomePage} exact />

I use redux-thunk to make sure I can use async fetch requests in my actions and those are setup something like this

Actions

export const loginSuccess = (user = {}) => ({
  type: 'LOGIN_SUCCESS',
  user
});

...

export const login = ({ userPhone = '', userPass = '' } = {}) => {
  return (dispatch) => {
    dispatch(loggingIn());
    const request = new Request('***', {
      method: 'post',
      body: queryParams({ user_phone: userPhone, user_pass: userPass }),
      headers: new Headers({
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
      })
    });
    fetch(request)
      .then((response) => {
        if (!response.ok) {
          throw Error(response.statusText);
        }

        dispatch(loggingIn(false));

        return response;
      })
      .then((response) => response.json())
      .then((data) => dispatch(loginSuccess(data.user[0])))
      .catch((data) => dispatch(loginError(data)));
  };
};

Reducers

export default (state = authenticationReducerDefaultState, action) => {
  switch (action.type) {
    ...
    case 'LOGIN_SUCCESS':
      return {
        ...state,
        authenticatedUser: action.user
      };
    default:
      return state;
  }
};

Where and how would I go about handling a redirect to wherever I was going before I was redirected to the login page, and how can I make sure this only happens on a success from the login fetch promise?

like image 373
Jordan Avatar asked Nov 28 '17 16:11

Jordan


People also ask

How do I redirect back to original url after successful login in laravel?

You can apply this filter to the routes that need authentication. Route::filter('auth', function() { if (Auth::guest()) { return Redirect::guest('login'); } }); What this method basically does it's to store the page you were trying to visit and it is redirects you to the login page. return Redirect::intended();


1 Answers

Your protected route is good. This will route you to the login route when a user is not authenticated.

In your high level react-router <router> you will need to nest: <Route path="/login" component={Login}/> to create a Login route.

Then in your Login route component you will render the UI to let users login.

class Login extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      userPhone: '',
      userPass: ''
    }
  }

  handleLogin() {
    this.props.login({ userPhone, userPass })
  }

  handlePhoneChange(event) {
    const { value } = event.currentTarget;
    this.setState({ userPhone: value });
  }

  handlePasswordChange(event) {
    const { value } = event.currentTarget;
    this.setState({ userPass: value });
  }
  
  render() {
    // this is where we get the old route - from the state of the redirect
    const { from } = this.props.location.state || { from: { pathname: '/' } } 
    const { auth } = this.props

    if (auth.redirectToReferrer) {
      return (
        <Redirect to={from}/>
      )
    }

    return (
      <div>
        <input
          value={this.state.userPhone}
          onChange={this.handlePhoneChange.bind(this)}
        />
        <input
          type="password"
          value={this.state.userPass}
          onChange={this.handlePasswordChange.bind(this)}
        />
        <button onClick={this.handleLogin.bind(this)}>Log in</button>
      </div>
    )
  }
}

This component will call the login action-creator function, (which will in turn call your API).

This will change the redux state if it is successful. This will re-render the Login component, and will do a redirect if auth.redirectToReferrer is true. (see code snippet above)

See https://reacttraining.com/react-router/web/example/auth-workflow for the docs.

like image 159
willwoo Avatar answered Oct 23 '22 05:10

willwoo