使用较旧的 aws-amplify-react
,为了在成功登录后将用户重定向到请求的 URL,我有:
<Authenticator
onStateChange={authState => {
if (authState === 'signedIn') {
const { from } = this.props.location.state || {
from: {
pathname: '/',
search: ''
}
};
this.props.history.replace(from.pathname + from.search);
}
}}
/>
来自documentation对于新版本 (@aws-amplify/ui-react
),我了解如何 access the auth state使用 useAuthenticator Hook ,但没有内置工具来处理此状态下的更改。是否不受支持,我现在需要实现自己的更改检测?
最佳答案
没有任何改变,钩子(Hook)useAuthenticator
可以返回表示当前authState
的route
:
const { route } = useAuthenticator(context => [context.route]);
if (route === "authenticated"){
const location = useLocation();
const history = useHistory();
const { from } = location.state || { from: { pathname: '/', search: '' } };
history.replace(from.pathname + from.search);
}
关于reactjs - 如何响应 @aws-amplify/ui-react 中身份验证状态的变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71431539/