Hello guys i created a new action to login user like this :
import * as types from './actionTypes';
import sessionApi from '../api/SessionApi';
import auth from '../auth/authenticator';
export function loginSuccess() {
return {type: types.LOG_IN_SUCCESS}
}
export function loginUser(credentials) {
return function(dispatch) {
return sessionApi.login(credentials).then(response => {
sessionStorage.setItem('token', response.token);
dispatch(loginSuccess());
}).catch(error => {
throw(error);
});
};
}
export function logOutUser() {
auth.logOut();
return {type: types.LOG_OUT}
}
and i create a session reducer for my auths like this :
import * as types from '../actions/actionTypes';
import initialState from './initialState';
export default function sessionReducer(state = initialState.session,
action) {
switch(action.type) {
case types.LOG_IN_SUCCESS:
// history.push('/restaurant')
return !!sessionStorage.token
default:
return state;
}
}
after the login success i want to redirect my user to another page bu using history.push but i don't know how to do that ?? i try to first import
import createBrowserHistory from "history/createBrowserHistory"
and then create a new const history like that :
export const history = createBrowserHistory({
forceRefresh: true
})
and then
history.push('/restaurant')
but after the action, that redirect me from /#home to /restaurant/#home .... and not to my right component . I have 2 routes file one for my main views like this :
const routes = [
{ path: '/', name: 'Home', component: Home },
{ path: '/login', name: 'Login', component: Login },
{ path: '/home', name: 'Landing', component: Landing },
{ path: '/dishes', exact: true, name: 'Detail', component: Dish },
{ path: '/dishes/detail', name: 'DishDetail', component: Detail },
{ path: '/checkout/registration', name: 'Restaurant', component:
Registration },
];
export default routes;
and one for all my restaurant views like this :
const routes = [
{ path: '/restaurant', exact: true, name: 'Restaurant', component:
RestrauntDashboard },
{ path: '/restaurant/dashboard', name: 'Restaurant Dashboard',
component: Dashboard },
{ path: '/restaurant/profile', name: 'Restaurant Dashboard', component:
Profile },
];
export default routes;
and this is my app.js :
class App extends Component {
render() {
return (
<HashRouter>
<Switch>
<Route path="/restaurant" name="Restaurant" component= .
{RestrauntDashboard} />
<Route path="/" name="Home" component={Home} />
</Switch>
</HashRouter>
);
}
}
export default App;
So finally i want to redirect the user in the '/restaurant/ path after he logged in , by using history.push , thank you for your help