amazon-archives / amazon-cognito-auth-js

The Amazon Cognito Auth SDK for JavaScript simplifies adding sign-up, sign-in with user profile functionality to web apps.
Apache License 2.0
423 stars 232 forks source link

Error using FireFox Quantum #61

Closed VittorioAccomazzi closed 6 years ago

VittorioAccomazzi commented 6 years ago

Hi There,

I love the tool and I used for my implementation. It works great on Chrome and Safari, however I'm having troubles in FireFox. The page properly redirect on the sign up, and then when redirected back on my page the post on token end point fails right away (see image) and if I refresh the page it then works fine.

The relevant code in my page is the following :

                   // Init Authentication
        auth = initCognitoSDK();
        var curUrl = window.location.href;
        console.log(curUrl);

        // here I'm assuming we are using 'code' instead of 'token' in Cognito. I'm also assuming
        // no other parameters are in the URL.
        var index = curUrl.indexOf("?code=");
        if( index > 0 ){
            var newUrl= curUrl.substring(0,index); // remove parameter on URL
            history.replaceState(null, null, newUrl); // rep-lace current URL
            auth.parseCognitoWebResponse(curUrl);
        } else {
            auth.getSession();
        }

and the POST failing is the following : mwsnap 2017-12-21-21_44_52

Any help/suggestion is welcome !!

vaccomazzi commented 6 years ago

I actually discover what is happening and how to work around. I'm not completely clear in all the aspect yet, but here below are my findings. In the description below please keep in mind that my project is based on the sample code Index.html :

1- Once the user is successfully signed in the onSuccess callback is invoked. This callback provides the session object which contains the tokens, which in turn contain the user information. However the username, contained in the token is not set in the auth object.

2- My code was periodically checking if the user was signed on. It was using for this purpose the function auth.getCachedSession().isValid(). The function auth.getCachedSession check if the username is defined, and it invalidate the session otherwise :

function getCachedSession() {
          if (!this.username) {
            return new _CognitoAuthSession2.default();
          }
.........

This was invalidating the session all the time.

3 - For some reason which I was not able to understand, the Chrome browser was able to retrieve the session from local storage, while FireFox was not.

The fix I implemented is simply to set the username once the onSuccess is invoked, as below :

            var payload = token.id.split('.')[1];
            var idToken = JSON.parse(atob(payload));
            var username=idToken["cognito:username"];
            auth.setUsername(username);

which prevent the auth.getCachedSession to invalidate the session and so the tokens are kept.

In short the following are still not clear to me : 1- Why upon receiving valid tokens the username is not set un the auth object. 2 - Why upon invalidating the session Chrome was able to find the valid tokens from the local storage, while FireFiox was not.

yuntuowang commented 6 years ago

Hi @vaccomazzi, thanks a lot for researching about this issue and provided the workaround. We indeed has a bug which will be pushed soon. We need to set username when caching tokens and scopes. This bug causes your "Why upon receiving valid tokens the username is not set un the auth object."

I am not entirely sure about your second question, will research more.

vaccomazzi commented 6 years ago

Thank you for looking in to my questions. Question #2 is regarding the different behaviour between Chrome and FireFox. I dug in to a bit and has something to do with the local storage, but I was not able to get to the bottom of it, because with the workaround which I put in place the two browsers behave consistently now.

Thank you for the good job in creating this library, I love this product !

yuntuowang commented 6 years ago

Hi @vaccomazzi, you are really really welcome! :)

Exactly as you mentioned, if you add the workaround which you provided, two browsers behave consistently. We will push this bug fix(update username when caching tokens and scopes) really soon, then this issue will be gone without any workaround, and two browsers will behave consistently too.

yuntuowang commented 6 years ago

Hi @vaccomazzi, our bug fix has been pushed! Please use version v1.1.1, you can download directly from github or use npm to install the latest version. Thanks!!

vaccomazzi commented 6 years ago

Thank you Crystal,

-Vittorio

From: Crystal Wang notifications@github.com Reply-To: aws/amazon-cognito-auth-js reply@reply.github.com Date: Thursday, January 4, 2018 at 4:27 AM To: aws/amazon-cognito-auth-js amazon-cognito-auth-js@noreply.github.com Cc: vaccomazzi vaccomazzi@imstsvc.com, Mention mention@noreply.github.com Subject: Re: [aws/amazon-cognito-auth-js] Error using FireFox Quantum (#61)

Hi @vaccomazzi, our bug fix has been pushed! Please use version v1.1.1, you can download directly from github or use npm to install the latest version. Thanks!!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.