Amplify-js: Session and signInUserSession null after authentication

Created on 7 Dec 2018  路  5Comments  路  Source: aws-amplify/amplify-js

* Which Category is your question related to? *
Amazon-cognito-identity

* What AWS Services are you utilizing? *
Cognito

* Provide additional details e.g. code snippets *
Hi, after I perform authentication with this method:

function AUTH_SignIn(pEmail, pPassword, pOnSuccess, pOnError) {
let authenticationData = {
Username : pEmail,
Password : pPassword,
};
let authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);

let userData = {
    Username : pEmail,
    Pool : AUTH_UserPool
};

let lUser = new AmazonCognitoIdentity.CognitoUser(userData);

lUser.authenticateUser(authenticationDetails, {
    onSuccess: function(result) {
        console.log(AUTH_UserPool.getCurrentUser());

        pOnSuccess(result);
    },

    onFailure: function(err) {
        pOnError(err);
    }
});

}

I fall into this type of problem:

[Log] t {username: "LucaColombano-1541716142229", pool: t, Session: null, client: t, signInUserSession: null, 鈥 (authentication.js, line 159)

with Session and signInUserSession set to null.

So when I call updateUserAttributes or getUserAttributes I receive a non authenticated error

Thank you,
Luca

Auth Cognito pending-close-response-required

Most helpful comment

Can you try the following code:

function AUTH_GetUserAttributes() {
    var user = AUTH_UserPool.getCurrentUser();
    user.getSession(function (err, session) {
        console.log(session);
        user.getUserAttributes(function (err, result) {
            if (err) {
                alert(err.message || JSON.stringify(err));
                return;
            }
            for (i = 0; i < result.length; i++) {
                console.log('attribute ' + result[i].getName() + ' has value ' + result[i].getValue());
            }
        });
    });
}

All 5 comments

@danieleleto can you try calling user.getSession() first before using updateUserAttributes or getUserAttributes? This is to load the session data from the cache into the user object.

Hi, excuse for the delay.

I've enclosed the code into the getSession method

function AUTH_GetUserAttributes() {
AUTH_UserPool.getCurrentUser().getSession(function (err, session) {
console.log(session);
session.getUserAttributes(function (err, result) {
if (err) {
alert(err.message || JSON.stringify(err));
return;
}
for (i = 0; i < result.length; i++) {
console.log('attribute ' + result[i].getName() + ' has value ' + result[i].getValue());
}
});
})
}

Session data are loaded but I can't call getUserAttributes on it.

If I use session.user.getUserAttributes(function (err, result) I receive a null error on user.

Can you try the following code:

function AUTH_GetUserAttributes() {
    var user = AUTH_UserPool.getCurrentUser();
    user.getSession(function (err, session) {
        console.log(session);
        user.getUserAttributes(function (err, result) {
            if (err) {
                alert(err.message || JSON.stringify(err));
                return;
            }
            for (i = 0; i < result.length; i++) {
                console.log('attribute ' + result[i].getName() + ' has value ' + result[i].getValue());
            }
        });
    });
}

Thank you very much,
now it works.

Regards, Luca

@danieleleto glad to hear that :) . I am going to close the issue now. Feel free to reopen if necessary.

Was this page helpful?
0 / 5 - 0 ratings