如何使用 React hooks 和 react-router 执行身份验证
How to perform authentication with React hooks and react-router
我正在尝试使用 react-router-dom
和 react hooks
在每次路由更改时对用户进行身份验证。
这个想法是,每次用户导航到一条路线时,系统都会进行 api 调用并对用户进行身份验证。
我需要实现这一点,因为我使用 react-redux
,并且在每次 window 重新加载时,redux 状态都不会保留。所以我需要再次将 isLoggedNow
属性设置为 true
:
const PrivateRoute = ({
component: Component,
checkOnEachRoute: checkReq,
isUserLogged,
...rest
}) => {
const [isLoggedNow, setLogged] = useState(isUserLogged);
useEffect(
() => {
const fetchStatus = async () => {
try {
await selectisUserLogged();
setLogged(true);
} catch (error) {
console.log(error);
}
};
fetchStatus();
},
[isUserLogged],
);
return (
<Route
{...rest}
render={props =>
isLoggedNow ? (
<div>
<Component {...props} />
</div>
) : (
<Redirect
to={{
pathname: '/login',
}}
/>
)
}
/>
);
};
然后我会像这样使用上面的PrivateRoute
:
function App(props) {
return (
<div>
<Switch location={props.location}>
<Route exact path="/login" component={Login} />
<PrivateRoute exact path="/sidebar" component={Sidebar} />
</Switch>
</div>
);
}
首先 isUserLogged
是 true
,但在 window 重新加载后我得到一个错误 Warning: Can't perform a React state update on an unmounted component.
那么我怎样才能做到这一点,所以在每次 window 重新加载时我都对用户进行身份验证?我正在寻找某种 componentWillMount
。
类似这样的东西(其中 isUserLogged
是来自 redux 的道具):
function PrivateRoute({ component: Component, isUserLogged, ...rest }) {
const [isLoading, setLoading] = useState(true);
const [isAuthenticated, setAuth] = useState(false);
useEffect(() => {
const fetchLogged = async () => {
try {
setLoading(true);
const url = new URL(fetchUrl);
const fetchedUrl = await fetchApi(url);
setAuth(fetchedUrl.body.isAllowed);
setLoading(false);
} catch (error) {
setLoading(false);
}
};
fetchLogged();
}, []);
return (
<Route
{...rest}
render={props =>
// eslint-disable-next-line no-nested-ternary
isUserLogged || isAuthenticated ? (
<Component {...props} />
) : isLoading ? (
<Spin size="large" />
) : (
<Redirect
to={{
pathname: '/login',
}}
/>
)
}
/>
);
}
我正在尝试使用 react-router-dom
和 react hooks
在每次路由更改时对用户进行身份验证。
这个想法是,每次用户导航到一条路线时,系统都会进行 api 调用并对用户进行身份验证。
我需要实现这一点,因为我使用 react-redux
,并且在每次 window 重新加载时,redux 状态都不会保留。所以我需要再次将 isLoggedNow
属性设置为 true
:
const PrivateRoute = ({
component: Component,
checkOnEachRoute: checkReq,
isUserLogged,
...rest
}) => {
const [isLoggedNow, setLogged] = useState(isUserLogged);
useEffect(
() => {
const fetchStatus = async () => {
try {
await selectisUserLogged();
setLogged(true);
} catch (error) {
console.log(error);
}
};
fetchStatus();
},
[isUserLogged],
);
return (
<Route
{...rest}
render={props =>
isLoggedNow ? (
<div>
<Component {...props} />
</div>
) : (
<Redirect
to={{
pathname: '/login',
}}
/>
)
}
/>
);
};
然后我会像这样使用上面的PrivateRoute
:
function App(props) {
return (
<div>
<Switch location={props.location}>
<Route exact path="/login" component={Login} />
<PrivateRoute exact path="/sidebar" component={Sidebar} />
</Switch>
</div>
);
}
首先 isUserLogged
是 true
,但在 window 重新加载后我得到一个错误 Warning: Can't perform a React state update on an unmounted component.
那么我怎样才能做到这一点,所以在每次 window 重新加载时我都对用户进行身份验证?我正在寻找某种 componentWillMount
。
类似这样的东西(其中 isUserLogged
是来自 redux 的道具):
function PrivateRoute({ component: Component, isUserLogged, ...rest }) {
const [isLoading, setLoading] = useState(true);
const [isAuthenticated, setAuth] = useState(false);
useEffect(() => {
const fetchLogged = async () => {
try {
setLoading(true);
const url = new URL(fetchUrl);
const fetchedUrl = await fetchApi(url);
setAuth(fetchedUrl.body.isAllowed);
setLoading(false);
} catch (error) {
setLoading(false);
}
};
fetchLogged();
}, []);
return (
<Route
{...rest}
render={props =>
// eslint-disable-next-line no-nested-ternary
isUserLogged || isAuthenticated ? (
<Component {...props} />
) : isLoading ? (
<Spin size="large" />
) : (
<Redirect
to={{
pathname: '/login',
}}
/>
)
}
/>
);
}