从AWS异步获得令牌以识别我的用户。呼叫等待令牌并将其保存到状态中。在同一个异步函数的最下方,是一个条件代码,可以将不同的内容发送给不同的用户。但是,返回的相同视图就像无法区分用户一样。
重构代码以将条件代码移至主要异步函数之外。
async componentDidMount() {
this.loadApp();
}
// Get the logged in users and remember them
loadApp = async () => {
await Auth.currentAuthenticatedUser()
.then(user => {
this.setState({
userToken: user.signInUserSession.accessToken.jwtToken
})
})
.catch(err => console.log(err))
Auth.currentSession()
.then(data => console.log(data))
.catch(err => console.log(err));
Auth.currentSession()
.then(data => {
this.setNav(data);
})
.catch(err => console.log(err));
// alert(this.state.User);
if (this.state.User === true) {
this.props.navigation.navigate(this.state.userToken ? 'AuthDelivery' :
'Auth')
} else {
this.props.navigation.navigate(this.state.userToken ? 'App' : 'Auth')
}
}
setNav(data) {
// alert(data);
DeliveryUser = data.accessToken.payload['cognito:groups'][0]
// alert(this.state.User);
if (DeliveryUser === 'DeliveryUser') {
this.state.User = true;
} else {
this.state.User = false;
}
}
//Expected this to properly identify the user, by setting the state to true or false. This is currently not happening is always sets to TRUE hence, returning the same view.
setNav(data) {
// alert(data);
DeliveryUser = data.accessToken.payload['cognito:groups'][0]
// alert(this.state.User);
if (DeliveryUser === 'DeliveryUser') {
this.state.User = true;
} else {
this.state.User = false;
}
}