成功登录后,aws-amplify 不保留会话

kar*_*jan 2 typescript reactjs webpack amazon-cognito aws-amplify

我正在研究 aws-serverless + react 应用程序。我的问题是使用aws-amplify. 身份验证正常,但不会保留会话。

登录中:

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

回复:

{
    "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": []
    }
}
Run Code Online (Sandbox Code Playgroud)

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

        try {
            await Auth.currentSession();
        }
        catch (e) {
            alert(e);
        }
Run Code Online (Sandbox Code Playgroud)

这给了我错误 No current user

我正在使用 react+typescript+webpack,我已经为它着急了 2 天了。

Dan*_*ñoz 6

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

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

当用户需要更新密码时,似乎 Amplify 不会持久化会话(您可以在challengeName字段的值中看到)。

你应该做的是让用户知道他们需要什么时候更新自己的密码challengeNameNEW_PASSWORD_REQUIRED

更新我使用的密码

await Auth.completeNewPassword(user, newPassword, {});
Run Code Online (Sandbox Code Playgroud)

user 来自

const user = await Auth.signIn(email, password);
Run Code Online (Sandbox Code Playgroud)

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