我正在使用AuthCheck组件来验证用户是否已登录,但是我还需要检查角色。
代码为
const App = () => {
const classes = useStyles();
const clientParam = { uri: process.env.REACT_APP_GRAPHQL_ENDPOINT };
const client = new ApolloClient(clientParam);
return (
<div className={classes.root}>
<ApolloProvider client={client}>
<CssBaseline />
<Router>
<Header />
<Switch>
<Route path="/b/:id" exact>
<AuthCheck fallback={<Login />}>
<MainContainer />
</AuthCheck>
</Route>
</Switch>
</Router>
</ApolloProvider>
</div>
);
};
“登录名”是表格,我在提交时运行
const auth = useAuth();
const firestore = useFirestore();
const handleSubmit = async (event) => {
event.preventDefault();
setError('');
try {
const result = await auth.signInWithEmailAndPassword(username, password);
const userData = (
await firestore.collection('users').doc(result.user.uid).get()
).data();
if (userData.userRole !== 'live_battle_admin') {
await auth.signOut();
throw new Error('You need to be a Live Battle Admin to sign in');
}
} catch (err) {
setError(err.message);
}
};
但是,这不起作用,因为用户在第一个呼叫解决后便立即登录,然后在我检查角色时注销。如何重写此代码以返回承诺?