amplify-js: TypeError: Cannot read properties of undefined (reading 'length') for getting the authorized user

Before opening, please confirm:

JavaScript Framework

React

Amplify APIs

Authentication

Amplify Categories

auth

Environment information

    "aws-amplify": "^5.0.18",

Describe the bug

This happens sproadically sometimes, but is heavily reproducable when reloading the page very quickly in a short time span (maybe 5-10 times within a few seconds). Then, we get the errors listed below, in currentUserPoolUser

Screenshot 2023-03-20 at 10 21 25 AM Screenshot 2023-03-20 at 10 19 55 AM

Expected behavior

The user is still logged in, or I would assume there is a concise error if they have been logged out.

Reproduction steps

This happens sproadically sometimes, but is heavily reproducable when reloading the page very quickly in a short time span (maybe 5-10 times within a few seconds).

Code Snippet

e.prototype.currentUserPoolUser = function(e) {
                var t = this;
                return this.userPool ? new Promise(function(n, r) {
                    t._storageSync.then(function() {
                        return h(t, void 0, void 0, function() {
                            var t, i, o, a, s, u, c, l = this;
                            return m(this, function(u) {
                                switch (u.label) {
                                case 0:
                                    if (!this.isOAuthInProgress())
                                        return [3, 2];
                                    return eY.debug("OAuth signIn in progress, waiting for resolution..."),
                                    [4, new Promise(function(e) {
                                        var t = setTimeout(function() {
                                            eY.debug("OAuth signIn in progress timeout"),
                                            w.X.remove("auth", n),
                                            e()
                                        }, 1e4);
                                        function n(r) {
                                            var i = r.payload.event;
                                            ("cognitoHostedUI" === i || "cognitoHostedUI_failure" === i) && (eY.debug("OAuth signIn resolved: " + i),
                                            clearTimeout(t),
                                            w.X.remove("auth", n),
                                            e())
                                        }
                                        w.X.listen("auth", n)
                                    }
                                    )];
                                case 1:
                                    u.sent(),
                                    u.label = 2;
                                case 2:
                                    if (!(t = this.userPool.getCurrentUser()))
                                        return eY.debug("Failed to get user from user pool"),
                                        r("No current user"),
                                        [2];
                                    u.label = 3;
                                case 3:
                                    return u.trys.push([3, 7, , 8]),
                                    [4, this._userSession(t)];
                                case 4:
                                    if (i = u.sent(),
                                    !(o = !!e && e.bypassCache))
                                        return [3, 6];
                                    return [4, this.Credentials.clear()];
                                case 5:
                                    u.sent(),
                                    u.label = 6;
                                case 6:
                                    if (a = this._config.clientMetadata,
                                    !(void 0 === (s = i.getAccessToken().decodePayload().scope) ? "" : s).split(" ").includes(eW))
                                        return eY.debug("Unable to get the user data because the " + eW + " is not in the scopes of the access token"),
                                        [2, n(t)];
                                    return t.getUserData(function(e, i) {
                                        return h(l, void 0, void 0, function() {
                                            var o, a, s, u, c, l, d;
                                            return m(this, function(d) {
                                                switch (d.label) {
                                                case 0:
                                                    if (!e)
                                                        return [3, 7];
                                                    if (eY.debug("getting user data failed", e),
                                                    !this.isSessionInvalid(e))
                                                        return [3, 5];
                                                    d.label = 1;
                                                case 1:
                                                    return d.trys.push([1, 3, , 4]),
                                                    [4, this.cleanUpInvalidSession(t)];
                                                case 2:
                                                    return d.sent(),
                                                    [3, 4];
                                                case 3:
                                                    return o = d.sent(),
                                                    r(Error("Session is invalid due to: " + e.message + " and failed to clean up invalid session: " + o.message)),
                                                    [2];
                                                case 4:
                                                    return r(e),
                                                    [3, 6];
                                                case 5:
                                                    n(t),
                                                    d.label = 6;
                                                case 6:
                                                    return [2];
                                                case 7:
                                                    for (u = 0,
                                                    a = i.PreferredMfaSetting || "NOMFA",
                                                    s = []; u < i.UserAttributes.length; u++) // Error here

Log output

// Put your logs below this line


aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 5
  • Comments: 22 (8 by maintainers)

Most upvoted comments

Thanks for the reply @micah-redwood. Upgrading to v6 of Amplify would be our recommendation for anyone still experiencing this or coming across this issue. Please feel free to reply back if there’s further issues with this error or additional context that was missed.

If anyone is still experiencing this issue in v5 on the latest version, please let us know. We’d recommend upgrading to the newest major version, v6 of Amplify, and seeing if this issue persists. We improved out TypeScript support and also updated the Auth API’s to be functional API’s with slightly different behavior (see here for more info).

Hello, I have the same issue from yesterday. Just after a reload of the page, I have the same error : image image For a unknown reason, the userData are empty : image

Every time I refresh the screen, I do a call to the currentAuthenticatedUser to check if the user is logged :

isLogged(): Observable<boolean> {
    return defer(() => Auth.currentAuthenticatedUser()
      .then(() => true)
      .catch((err: any) => {
        console.log(err);
        return false;
      }))
      .pipe(
        tap((logged) => this._loggedSbj.next(logged))
      );
  }

and the error is thrown.

This is the first time, so not sure I can reproduce this error.

Bump. Running into the same issue in production.

bump on this issue, still causing lots of user sign in errors (and its not a catchable error, as its not from any specific call that I can tell, since it seems to be coming from the init) @cwomack 😃

removed all calls of anything Auth. from a page, and the error still occurs. the SSR call return the correct user, and then something on client side (I assume the Amplify configure call?).

export const fetchUser = async (SSR) => {
    try {
        return (await SSR.Auth.currentAuthenticatedUser()).signInUserSession.idToken.payload
    } catch (e) {
        console.log("e", e);
        return null;
    }
};

const LandingPage = ({ serverUser }) => {
    return (
       <div />
    );
};

export const getServerSideProps = async context => {
    const SSR = withSSRContext(context);
    const user = await fetchUser(SSR);
    const { code } = context.query;

    return {
        props: {
                serverUser: user
            },
        },
};

still throws the current error. This is a pretty big bug from our end, as it happens to a good amount of our users, and since its not a catchable error (since its not anything we call), we are not able to properly fix it for the useres that run into it. @cwomack @stocaaro @nadetastic

Screenshot 2023-04-16 at 6 15 09 PM