I have a simple use case which within the React-Router 4 and Redux environment seems pretty difficult to achieve.
When a user logs in, a redux promise action is fired off which returns an API token, expiry date and some other info. When and if the promise is fulfilled I would then want to redirect the user to another page.
I cannot for figure out how to redirect a user programmatically using the above architecture. I want to do it within Action.js, but it feels that it can only be done in Reducers.js. Even though I am not sure even how to start.
Best way I have found so far is to have a Redirect component in my view, and have the promise set the loggedIn state to true, if the loggedIn prop is true on the view it will then return the redirect prop which will then redirect the user to the desired page. Somehow this feels like a lot of extra code for a simple use case.
Any advice would be helpful. Thank you.
Action.js:
import axios from 'axios';
export function login(email, password) {
return {
type: 'LOGIN_USER',
payload: axios.post('http://localhost:3114/api/users/authenticate',
{
email,
password,
}),
};
}
Reducers.js
// Promise
// pending
// fulfilled
// rejected
const initalState = {
token: null,
expiryDate: '',
loading: false,
error: null,
};
// REDCUER
function loginReducer(state = initalState, action) {
switch (action.type) {
case 'LOGIN_USER_PENDING':
return { ...state, loading: true };
case 'LOGIN_USER_FULFILLED':
return {
...state,
loading: false,
token: action.payload.data.token,
expiryDate: action.payload.data.expires,
loggedIn: true,
};
case 'LOGIN_USER_REJECTED':
return { ...state, loading: false, error: `${action.payload}` };
default:
return state;
}
}
export default loginReducer;
LoginView.js
render() {
const { data, login, loggedIn } = this.props;
if (data.loggedIn) {
return <Redirect to="/users" push />;
}
return (
...
)
}
Regards, Emir