使用旧的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);
}
}}
/>
从新版本(@aws-amplify/ui-react
(的文档中,我看到了如何使用useAuthenticator
钩子访问身份验证状态,但没有内置的工具来处理此状态下的更改。它是否不受支持,并且我现在需要实现自己的更改检测?
钩子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);
}