[英]Wait a result of a async function before render react routes
I'm trying to implement a auth service inside my SPA with React in the routes file.我正在尝试在路由文件中使用 React 在我的 SPA 中实现身份验证服务。 When a User try to access some private path, the function should verify if the token inside machine is valid.
当用户尝试访问某个私有路径时,function 应该验证机器内的令牌是否有效。 But I'm getting a problem in this logic, the function is passing false value to private route.
但是我在这个逻辑中遇到了一个问题,function 正在将错误值传递给私有路由。
My Route File:我的路线文件:
const Routes = () => {
var [authedIn, setAuthedIn] = useState(false);
var authedNow = useRef(authedIn);
const verifyLog = async () => {
if (await AuthUser().then(res => res).catch(res => res) === true) {
setAuthedIn = true;
authedNow.current = true;
console.log('log in');
console.log(authedNow.current);
return true;
}
else {
setAuthedIn = false;
authedNow.current = false;
console.log('log of');
return false;
}
}
useEffect(() => {
verifyLog()
}, [authedNow.current]);
console.log(authedNow.current);
return (
<BrowserRouter>
<Switch>
<Route exact path="/" component={HomeScreen} />
<Route exact path="/register" component={RegisterScreen} />
<Route exact path="/login" component={LoginScreen}/>
<PrivateRouter exact path="/notes" component={NotesScreen} isAuth={authedNow.current}/>
<PrivateRouter exact path="/users/edit" component={UsersEditScreen} isAuth={authedNow.current}/>
</Switch>
</BrowserRouter>
)};
export default Routes;
AuthUser file:验证用户文件:
const AuthUser = async () => {
const response = await AuthService.verifyToken();
if (response.status === 200) {
return true;
} else {
localStorage.clear();
return false;
}
}
Is there a way to render after the result of the async
function?有没有办法在
async
function 的结果之后进行渲染?
To cause a re-render after the async function is complete you need to update state.要在异步 function 完成后重新渲染,您需要更新 state。 I am not super knowledgeable on using refs, but you are using the
setAuthedIn
function incorrectly.我对使用 refs 不是很了解,但是您错误地使用了
setAuthedIn
function。 You need to call it with a parameter instead of setting it equal to a boolean.您需要使用参数调用它,而不是将其设置为 boolean。
Try changing your verifyLog
function to look like this:尝试将
verifyLog
function 更改为如下所示:
const verifyLog = async () => {
if (await AuthUser().then(res => res).catch(res => res) === true) {
setAuthedIn(true); // <- call the setter function to update authedIn state
return true;
}
else {
setAuthedIn(false);
return false;
}
}
since you already return a boolean value from your async await AuthUser
, you could simplify verifyLog to:由于您已经从 async await
AuthUser
返回了 boolean 值,因此您可以将 verifyLog 简化为:
const verifyLog = async () => {
const isAuthorized = await AuthUser()
setAuthedIn(isAuthorized);
authedNow.current = isAuthorized;
return isAuthorized;
}
}
I would suggest to wrap AuthUser
into a try catch block, where catch block returns false for consistency.我建议将
AuthUser
包装到 try catch 块中,其中 catch 块返回 false 以保持一致性。
If it keeps setting a falsey value to state then it would be good to check response.status
value.如果它一直将错误值设置为 state 那么最好检查
response.status
值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.