简体   繁体   English

刷新页面后如何留在受保护的路线上。 反应路由器 5.2.0

[英]How to stay on the protected route, after refreshing page. React router 5.2.0

I have public and protected routes, which implemented in the following logic我有公共和受保护的路线,它们在以下逻辑中实现

   useEffect(() => {
      checkIfAuthenticated(); //This function checks if the token is in localStorage
     // Initial state isAuthenticated = false
    }, []);
   const newHistory = useHistory()

   <Router history={newHistory}>
    {isAuthenticated ? (
      <Layout>
        <Switch>
          <Route
             exact
             path="/clients"
             component={Clients}
            />
          <Route
             exact
             path="/staff"
             component={Staff}
            />
          <Redirect to="/clients" />
        </Switch>
      </Layout>
       ) : (
      <>
       <Route path="/login" component={Login} />
       <Redirect to="/login" />
      </>)}
    </Router>

//store
checkIfAuthenticated() {
    const accessToken = localStorage.getItem("access_token");
    const refreshToken = localStorage.getItem("refresh_token");
    if (accessToken && refreshToken) {
      this.isAuthenticated = true;
    }
  }

The problem is when I refresh the page on the route /staff?page=1, the app initializes from scratch and I am redirected to the /clients route.问题是当我刷新路由 /staff?page=1 上的页面时,应用程序从头开始初始化,我被重定向到 /clients 路由。 Is there any way to handle this case?有什么办法可以处理这种情况吗? Ie when I refresh the private route, I should stay on the same page.即当我刷新私人路线时,我应该留在同一页面上。 Maybe I should save the current route to the localStorage?也许我应该将当前路由保存到 localStorage?

You must use redux-persist library to keep the value of isAuthenticated variable in localStorage after refresh.您必须使用redux-persist库在刷新后将isAuthenticated变量的值保留在 localStorage 中。

Something like this:像这样的东西:

import { persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';

import userReducer from './userReducer';

const persistConfig = {
  key: 'user',
  storage,
  whitelist: ['isAuthenticated'],
};

const persistedReducer = persistReducer(persistConfig, userReducer);

If you are sure about persistance of variables in localStorage check this as well:如果您确定 localStorage 中变量的持久性,请同时检查:

const accessToken = localStorage.getItem("access_token");
const refreshToken = localStorage.getItem("refresh_token"); 
const [isAuthenticated, setIsAuthenticated] = useState(false);
useEffect(() => {
  if (accessToken && refreshToken) {
    setIsAuthenticated(true);
  }
}, [accessToken , refreshToken ]);

const newHistory = useHistory()

<Router history={newHistory}>
{isAuthenticated ? (
  <Layout>
    <Switch>
      <Route
         exact
         path="/clients"
         component={Clients}
        />
      <Route
         exact
         path="/staff"
         component={Staff}
        />
      <Redirect to="/clients" />
    </Switch>
  </Layout>
   ) : (
  <>
   <Route path="/login" component={Login} />
   <Redirect to="/login" />
  </>)}
</Router>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM