# UnauthorizedError not returning error

**URL:** <https://community.auth0.com/t/unauthorizederror-not-returning-error/21375>\
**Category:** Get Help\
**Tags:** rules\
**Created:** [February 15, 2019, 7:08pm UTC](https://community.auth0.com/t/unauthorizederror-not-returning-error/21375 "2019-02-15T19:08:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul.bennett](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/paul.bennett/32/11109_2.png) [@paul.bennett](https://community.auth0.com/u/paul.bennett)\
**Post date:** [February 15, 2019, 7:08pm UTC](https://community.auth0.com/t/unauthorizederror-not-returning-error/21375/1 "2019-02-15T19:08:07Z")

</div>

I have a rule:

```js
function (user, context, callback) {
  var _ = require('lodash');
  var groupsFromIdP = user.groups || [];
  console.log(context);

  var groups = ['GeneralUser'];
  groups = _.union(groups, groupsFromIdP); 

  var userEmail = user.email.toString();
  var sepIndex = userEmail.indexOf('@');
  sepIndex = sepIndex + 1;

  if (sepIndex >= 0) {
    var userDomain = userEmail.substring(sepIndex);
    
    var genericDomains = ['asx.com.au', 'accenture.com', 'bnpparibas.com', 'broadridge.com', 'bursamalaysia.com', 'calastone.com', 'citi.com', 'db.com', 'dtcc.com', 'deutsche-boerse.com', 'euroclear.com', 'fanniemae.com', 'gft.com', google.com', 'gs.com', 'hkex.com.hk', 'humana.com', 'theice.com', 'jpmorgan.com', 'lseg.com', 'pnc.com', 'sgx.com', statestreet.com', 'strate.co.za', 'td.com'];
    
    if (genericDomains.indexOf(userDomain) >=0 )
      groups = _.union(['GenericCustomer'], groups); 

    groups = _.union(['SDKUser'], groups); 
    groups = _.union(['validated_people'], groups); 

  }
  
  context.idToken['<removed - link>'] = groups;

  var CLIENT_SECRET = 'lLXmX5CYiryn5ZG0IZe739-ZjVscNoFzkPAV2QwcvdPM-_sllVRPFFvgKh-s08zf';
  var CLIENT_ID = 'nCMYFjoep8SjJn0Ot1xyc2t1cDS73jcp';

  //Copies user profile attributes needed in the API (equivalent to `scope`)
  var api_user = {
    user_id: user.email,
    email: user.email,
    name: user.name
  };

  var options = {
    subject: user.email,
    expiresInMinutes: 600, //Should be greater than the SAML token expiration
    audience: CLIENT_ID,
    issuer: '<removed - link>'
  };

  context.idToken['<removed - link>'] = jwt.sign(api_user, 
      new Buffer(CLIENT_SECRET, 'base64'),
      options);

  //console.log(context);

  // if available, use upn as NameID
  if (context.clientName === "DA-Zendesk") {
      if (user.email) {
           context.samlConfiguration.mappings = {
             "<removed - link>'r": "email"
             };
      }
  }

  if (context.connection !== 'digitalasset-com' && groups.indexOf('validated_people') < 0) {
    return callback(new UnauthorizedError("Your account is pending approval."));
  }
  
  if (context.connection !== 'digitalasset-com' && context.clientName === 'SDK-bintray' && (groups.indexOf('CustomerDBG') 0 || groups.indexOf('CustomerBBK') > 0)) {
    return callback(new UnauthorizedError("You are not authorised to access this service."));
  }
  
  if(userEmail.indexOf('+') >= 0) {
    console.log('email has + - unauthorized: ' + userEmail);
    return callback(new UnauthorizedError("Email addresses containing \'+\' are not valid"));
  }
     
  if(!user.email_verified) {
    context.redirect = {
        url: "https://docs.digitalasset.com/50x.html?error=unvalidated%20email&rror_description=Please%20validate%20your%20email%20address"
    };
    // return callback(new UnauthorizedError("Please check your email to validate your account."));
  }
    
  return callback(null, user, context);
}

```

The relevant part is the test for a ‘+’ sign in an email. The test is correct, and a Real-Time Webtask log shows that the console log does appear i.e. the test is correct. But - the rule does NOT cause an UnauthorizedError, and allows the authentication to proceed. Why would that occur?

---

<div class="post-metadata">

**Author:** ![nicolas\_sabena](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/nicolas_sabena/32/6064_2.png) [@nicolas\_sabena](https://community.auth0.com/u/nicolas_sabena)\
**Post date:** [February 15, 2019, 8:23pm UTC](https://community.auth0.com/t/unauthorizederror-not-returning-error/21375/2 "2019-02-15T20:23:40Z")

</div>

What if you create another rule that just simply returns an `UnauthorizedError`? Like this:

```plaintext
function (user, context, callback) {
  return callback(new UnauthorizedError("Email addresses containing \'+\' are not valid"));
}

```

What flow are you trying to use (i.e. regular interactive login, sign up, resource owner password credentials,…)?

As a general good practice, I’d recommend splitting different logic into different rules. E.g.

- “Redirect if email is not verified”
- “Check for valid email addresses”
- “Check authorization for SDK-bintray”
- and so on

---

<div class="post-metadata">

**Author:** ![paul.bennett](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/paul.bennett/32/11109_2.png) [@paul.bennett](https://community.auth0.com/u/paul.bennett)\
**Post date:** [February 19, 2019, 6:44pm UTC](https://community.auth0.com/t/unauthorizederror-not-returning-error/21375/3 "2019-02-19T18:44:02Z")

</div>

I changed the test to perform a redirect - which is actually more of what is wanted - and it now works. Will also take your point about splitting up the rules. Thanks

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [March 6, 2019, 6:44pm UTC](https://community.auth0.com/t/unauthorizederror-not-returning-error/21375/4 "2019-03-06T18:44:03Z")

</div>

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.
