【发布时间】:2021-11-02 14:39:16
【问题描述】:
我在那里使用reactjs,当用户注销时,本地存储和会话被正常清除
但是当用户单击浏览器上的返回按钮时,用户仍然可以访问仪表板甚至可以管理它
如何预防?
注销操作:
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!!!');
});
};
检查登录操作:
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());
};
};
私有路由配置:
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} />
}
}}
/>
);
};
路由器:
<Switch>
<ConfigRoute exact path="/dashboard" component={Dashboard} />
<ConfigRoute exact path="/product" component={ProductList} />
<Redirect to="/auth/login" />
</Switch>
【问题讨论】:
标签: javascript node.js reactjs firebase react-router