Repository: auth0/auth0.js
Pull request #443 (2017-05-24 16:25:41)

Sending all /co/authenticate errors to the error callback


Patch:
diff --git a/src/web-auth/cross-origin-authentication.js b/src/web-auth/cross-origin-authentication.js
index 5e28065..b465910 100644
--- a/src/web-auth/cross-origin-authentication.js
+++ b/src/web-auth/cross-origin-authentication.js
@@ -53,21 +53,10 @@ CrossOriginAuthentication.prototype.login = function(options, cb) {
   this.request.post(url).withCredentials().send(authenticateBody).end(function(err, data) {
     if (err) {
       var errorObject = (err.response && err.response.body) || {
-        error: 'Request Error',
+        error: 'request_error',
         error_description: JSON.stringify(err)
       };
-      var authorizationErrorCodes = ['access_denied'];
-      var isAuthorizationError = authorizationErrorCodes.indexOf(errorObject.error) > -1;
-      if (cb && isAuthorizationError) {
-        return cb(errorObject);
-      }
-      var redirectUrl = _this.baseOptions.redirectUri || options.redirectUri;
-      var errorHash =
-        '#error=' +
-        encodeURI(errorObject.error) +
-        '&error_description=' +
-        encodeURI(errorObject.error_description);
-      return windowHelper.redirect(redirectUrl + errorHash);
+      return cb(errorObject);
     }
     options = objectHelper.blacklist(options, ['username', 'password']);
     var authorizeOptions = objectHelper


Test patch:
diff --git a/test/web-auth/cross-origin-authentication.test.js b/test/web-auth/cross-origin-authentication.test.js
index 1ea3832..7117556 100644
--- a/test/web-auth/cross-origin-authentication.test.js
+++ b/test/web-auth/cross-origin-authentication.test.js
@@ -141,7 +141,7 @@ describe('auth0.WebAuth.crossOriginAuthentication', function() {
     context(
       'should call callback and not redirect to authorize when it is an authentication error',
       function() {
-        it('access_denied', function(done) {
+        it('with error_description', function(done) {
           stub(request, 'post', function(url) {
             expect(url).to.be('https://me.auth0.com/co/authenticate');
             return new RequestMock({
@@ -158,8 +158,8 @@ describe('auth0.WebAuth.crossOriginAuthentication', function() {
                 cb({
                   response: {
                     body: {
-                      error: 'access_denied',
-                      error_description: 'access denied'
+                      error: 'any_error',
+                      error_description: 'any error'
                     }
                   }
                 });
@@ -174,89 +174,49 @@ describe('auth0.WebAuth.crossOriginAuthentication', function() {
               anotherOption: 'foobar'
             },
             function(err) {
-              expect(err).to.be.eql({ error: 'access_denied', error_description: 'access denied' });
+              expect(err).to.be.eql({ error: 'any_error', error_description: 'any error' });
               expect(_this.webAuthSpy.authorize.called).to.be.eql(false);
               done();
             }
           );
         });
-      }
-    );
-    it('should call /co/authenticate and redirect to options.redirectUri when an error WITH description occur', 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({
-              response: {
-                body: {
-                  error: 'Ops',
-                  error_description: 'Something happened'
-                }
+        it('without error_description', 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({ some: 'error' });
               }
             });
-          }
-        });
-      });
-      stub(windowHelper, 'redirect', function(url) {
-        expect(url).to.be.equal(
-          'https://page.com/callback#error=Ops&error_description=Something%20happened'
-        );
-        done();
-      });
-
-      this.co.login({
-        username: 'me@example.com',
-        password: '123456',
-        anotherOption: 'foobar'
-      });
-    });
-    it('should call /co/authenticate and redirect to options.redirectUri when an error WITHOUT description occur', 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({
-              foo: 'bar'
-            });
-          }
+          });
+          var _this = this;
+          this.co.login(
+            {
+              username: 'me@example.com',
+              password: '123456',
+              anotherOption: 'foobar'
+            },
+            function(err) {
+              expect(err).to.be.eql({
+                error: 'request_error',
+                error_description: '{"some":"error"}'
+              });
+              expect(_this.webAuthSpy.authorize.called).to.be.eql(false);
+              done();
+            }
+          );
         });
-      });
-      stub(windowHelper, 'redirect', function(url) {
-        expect(url).to.be.equal(
-          'https://page.com/callback#error=Request%20Error&error_description=%7B%22foo%22:%22bar%22%7D'
-        );
-        done();
-      });
-
-      this.co.login({
-        username: 'me@example.com',
-        password: '123456',
-        anotherOption: 'foobar'
-      });
-    });
+      }
+    );
   });
   context('callback', function() {
     before(function() {