gpt4 book ai didi

reactjs - aws-amplify 在成功登录后不保留 session

转载 作者:行者123 更新时间:2023-12-01 21:55:15 24 4
gpt4 key购买 nike

我正在开发 aws-serverless + React 应用程序。我的问题是使用 aws-amplify 进行身份验证。身份验证正常,但 session 未保留。

登录:

 await Auth.signIn(this.state.email, this.state.password)
.then(x => {
console.log(JSON.stringify(x))
})
.catch(e => alert(e.message));

响应:

{
"username": "xxxxx",
"pool": {
"userPoolId": "eu-central-1_xxxxx",
"clientId": "xxxxx",
"client": {
"endpoint": "https://cognito-idp.eu-central-1.amazonaws.com/",
"userAgent": "aws-amplify/0.1.x js"
},
"advancedSecurityDataCollectionFlag": true,
"storage": {
"loglevel:webpack-dev-server": "INFO"
}
},
"Session": "xxxxx",
"client": {
"endpoint": "https://cognito-idp.eu-central-1.amazonaws.com/",
"userAgent": "aws-amplify/0.1.x js"
},
"signInUserSession": null,
"authenticationFlowType": "USER_SRP_AUTH",
"storage": {
"loglevel:webpack-dev-server": "INFO"
},
"keyPrefix": "CognitoIdentityServiceProvider.xxxxx",
"userDataKey": "CognitoIdentityServiceProvider.xxxxx.xx@x.pl.userData",
"challengeName": "NEW_PASSWORD_REQUIRED",
"challengeParam": {
"userAttributes": {
"email_verified": "true",
"phone_number_verified": "true",
"phone_number": "xxxxx",
"email": "xx@x.pl"
},
"requiredAttributes": []
}
}

之后我让它等待几秒钟。并尝试获取 session 数据:

        try {
await Auth.currentSession();
}
catch (e) {
alert(e);
}

这给了我错误 No current user

我正在使用 react+typescript+webpack,这两天一直在抨击它。

最佳答案

我今天刚遇到这个问题。我从 AWS 控制台在(新)用户池中创建了一个新用户,但登录后, session 没有保留(每次刷新页面时,Auth.currentSession() 都会引发错误,指示没有经过身份验证的用户或类似的东西)。

大约一个小时后,我找到了解决方案...

当用户需要更新密码时,Amplify 似乎不会保留 session (您可以在 challengeName 字段的值中看到这一点)。

challengeNameNEW_PASSWORD_REQUIRED 时,您应该做的是让用户知道他们需要更新密码。

更新我使用的密码

await Auth.completeNewPassword(user, newPassword, {});

用户来自

const user = await Auth.signIn(email, password);

这样做之后, session 将被持久化。

关于reactjs - aws-amplify 在成功登录后不保留 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58157268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com