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
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)));
};
};
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?
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();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With