【发布时间】:2018-12-22 07:12:29
【问题描述】:
我正在使用 Amazon 的 Cognito 用户池来启动 SAML SSO 身份验证。我正在使用aws-amplify,这是一个 JavaScript 库,用于使用云服务和前端的 ReactJS 进行应用程序开发。尝试使用 currentSession() 函数似乎存在一些问题。使用包的 Auth 模块的 Auth.siginIn() 函数时,我的会话似乎没有保存会话。我已经使用我在用户池中设置的当前测试用户进行了测试并成功登录。我之所以要使用Auth.currentSession() 函数是为了验证我的应用程序中每个视图的会话。下面你可以看到,在用户验证登录后,我将它们发送到我的仪表板视图,然后在组件安装后尝试验证会话,但它返回一个错误,没有找到用户。下面是我使用的代码。我查看了 aws-amplify 在其网站上的文档,但似乎无法确定问题所在。
注意:Redux 被用于存储来自 setUserData 和 setAuthToken (session 登录后回复)
Login.js
handleSubmit = (event) => {
event.preventDefault();
Auth.signIn(this.state.sso, this.state.password)
.then((res) => {
this.props.authUser(true)
this.props.setUserData(res.username)
this.props.setAuthToken(res.Session)
})
.then(() => {
this.props.history.push("/Dashboard")
})
//catches err
.catch((e) => {
console.log(e);
alert(e.message)
this.props.authUser(false)
})
}
一旦用户登录,用户应该被推送到仪表板。在组件生命周期中调用 componentDidMount() 后,我想检查当前会话以查看是否仍在会话中。
Dashboard.js
componentDidMount(){
debugger;
console.log(Auth);
Auth.currentSession().then((res) => {
console.log(res);
}).catch((e) => {
alert(e)
})
this.props.toggleError(false);
this.props.toggleNotify()
}
【问题讨论】:
标签: javascript reactjs amazon-web-services aws-cognito aws-amplify