I'm using reactjs there, when the user logout, the localstorage and session was cleared normally
But when user click the back button on the browser, the user can still accessed the dashboard even can manage it
How to prevent this?
Logout Action:
export const logoutUserAction = () => dispatch => {
//! Loading
dispatchLoading(dispatch, LOGOUT_USER_ACTION);
firebase
.auth()
.signOut()
.then(() => {
window.localStorage.removeItem('user');
dispatchSuccess(dispatch, LOGOUT_USER_ACTION, 'Success Logout!!!');
})
.catch(() => {
window.localStorage.removeItem('user');
dispatchError(dispatch, LOGOUT_USER_ACTION, 'Failed Logout!!!');
});
};
Check Login Action:
export const checkLoginAction = history => dispatch => {
//! Loading
dispatchLoading(dispatch, CHECK_LOGIN_ACTION);
const user = JSON.parse(window.localStorage.getItem('user'));
if (user) {
firebase
.database()
.ref(`users/${user.uid}`)
.once('value')
.then(snapshot => {
if (snapshot.val()) {
window.localStorage.setItem('user', JSON.stringify(snapshot.val()));
history.push('/dashboard');
dispatchSuccess(dispatch, CHECK_LOGIN_ACTION, snapshot.val());
};
};
Private Route Config:
function ConfigRoute({ component: Component, dispatch, checkLoginLoading, checkLoginResult, ...rest }) {
const history = useHistory();
useEffect(() => {
dispatch(checkLoginAction(history));
}, [dispatch, history]);
return (
<Route
{...rest}
render={(props) => {
if (checkLoginLoading) {
return <LoadingIndicator />
} else {
return <Component {...props} />
}
}}
/>
);
};
Router:
<Switch>
<ConfigRoute exact path="/dashboard" component={Dashboard} />
<ConfigRoute exact path="/product" component={ProductList} />
<Redirect to="/auth/login" />
</Switch>