Repository: auth0/auth0.js
Pull request #668 (2018-02-15 20:57:38)

Fixed error handling for auth in poopup mode


Patch:
diff --git a/src/web-auth/cross-origin-authentication.js b/src/web-auth/cross-origin-authentication.js
index 4e540d9..7064697 100644
--- a/src/web-auth/cross-origin-authentication.js
+++ b/src/web-auth/cross-origin-authentication.js
@@ -81,7 +81,7 @@ CrossOriginAuthentication.prototype.login = function(options, cb) {
     var key = createKey(_this.baseOptions.rootUrl, data.body.co_id);
     theWindow.sessionStorage[key] = data.body.co_verifier;
     if (popupMode) {
-      _this.webMessageHandler.run(authorizeOptions, cb);
+      _this.webMessageHandler.run(authorizeOptions, responseHandler(cb));
     } else {
       _this.webAuth.authorize(authorizeOptions);
     }


Test patch:
diff --git a/test/web-auth/cross-origin-authentication.test.js b/test/web-auth/cross-origin-authentication.test.js
index 6a67d1c..1b309bd 100644
--- a/test/web-auth/cross-origin-authentication.test.js
+++ b/test/web-auth/cross-origin-authentication.test.js
@@ -125,6 +125,53 @@ describe('auth0.WebAuth.crossOriginAuthentication', function() {
         }
       );
     });
+    it('should map error correctly when popup:true', function(done) {
+      stub(request, 'post', function(url) {
+        expect(url).to.be('https://me.auth0.com/co/authenticate');
+        return new RequestMock({
+          body: {
+            client_id: '...',
+            credential_type: 'password',
+            username: 'me@example.com',
+            password: '123456'
+          },
+          headers: {
+            'Content-Type': 'application/json'
+          },
+          cb: function(cb) {
+            cb(null, {
+              body: {
+                login_ticket: 'a_login_ticket',
+                co_verifier: 'co_verifier',
+                co_id: 'co_id'
+              }
+            });
+          }
+        });
+      });
+      stub(WebMessageHandler.prototype, 'run', function(options, callback) {
+        callback({ error: 'any error', error_description: 'a huge error string' });
+      });
+      this.co.login(
+        {
+          username: 'me@example.com',
+          password: '123456',
+          anotherOption: 'foobar',
+          popup: true
+        },
+        function(err) {
+          expect(err).to.be.eql({
+            original: {
+              error: 'any error',
+              error_description: 'a huge error string'
+            },
+            code: 'any error',
+            description: 'a huge error string'
+          });
+          done();
+        }
+      );
+    });
     it('should call /co/authenticate with realm grant and redirect to /authorize with login_ticket when realm is used', function() {
       stub(request, 'post', function(url) {
         expect(url).to.be('https://me.auth0.com/co/authenticate');
@@ -268,7 +315,10 @@ describe('auth0.WebAuth.crossOriginAuthentication', function() {
             },
             function(err) {
               expect(err).to.be.eql({
-                original: { error: 'any_error', error_description: 'a super big error message description' },
+                original: {
+                  error: 'any_error',
+                  error_description: 'a super big error message description'
+                },
                 code: 'any_error',
                 description: 'a super big error message description'
               });