Auth0 Home Blog Docs

Why does my authentication rule sometimes NOT get called?

Why does my authentication rule sometimes NOT get called ?
For certain user , the end point for my custom rule does not get called.
I need the the rule to be call for ALL authentication requests.
And insight would be appreciated.
Thanks
Ray

Hi @ray,

Can you show us the code for the rule that is failing?

Hi !
Thanks for the quick response :slight_smile:

I probably should have explained better.
The code is really not failing.
I have a rule that calls my web service that adds information to the user after authentication.

For most users , it calls my service and returns the user info as below…Stringified JSON…

{“sub”:“auth0|xxxxxxxxxxxxxxxxxxxx”,“http://xxxxxxx.com/xxxxxx":{“ID”:“4”,“role”:“web”,“FirstName”:“jxxxxf@twc.com”,"LastName”:null}}

My rule added the attributes starting with “http …”

What’s confusing is that certain users authenticate without the rule being called and return this…
{“sub”:“auth0|xxxxxxxxxxxxxxxxxxxx”}

I would assume that the rule would always be called.
I must be missing something :slight_smile:

Thanks for your help

Hmmm a few cents to add to this. Not sure if one is related to the other but rules are run only at successful login

Hi Thanks for your help.
Yes, this does happen after a successful login.

Hi @ray,

Are you able to share the rule (code) with us? It’s a long shot but sometimes seeing the code helps.

Just to confirm: the ‘failure’ appears to be random? It’s not something you can reproduce consistently?

Yep are you able to identify some pattern for which users it appears to be like that?

Thanks everyone…
I’m having a hard time seeing a pattern in the users - however it does seem to be the same ones.

I copied the code from the example regular web app - using Javascript SDK. (See below…)

I only modified the code to output the user info. On the html page I added a div to show the user info…
See the alerts, etc around line 100.
In particular the call to webAuth.client.userInfo(authResult… Inside handleAuthentication(

Code starts here …
// app.js
window.addEventListener(‘load’, function() {
var idToken;
var accessToken;
var expiresAt;

var content = document.querySelector(’.content’);
var loadingSpinner = document.getElementById(‘loading’);
content.style.display = ‘block’;
// loadingSpinner.style.display = ‘none’;

var webAuth = new auth0.WebAuth({
domain: ‘jitdocs.auth0.com’,
clientID: ‘txxxx’,
responseType: ‘token id_token’,
redirectUri: “http://xxxx”,
scope: ‘openid’,
leeway: 60
});

var loginStatus = document.querySelector(’.container h4’);
var loginView = document.getElementById(‘login-view’);
var homeView = document.getElementById(‘home-view’);

// buttons and event listeners
var homeViewBtn = document.getElementById(‘btn-home-view’);
var loginBtn = document.getElementById(‘btn-login’);
var logoutBtn = document.getElementById(‘btn-logout’);

homeViewBtn.addEventListener(‘click’, function() {
homeView.style.display = ‘inline-block’;
loginView.style.display = ‘none’;
});

loginBtn.addEventListener(‘click’, function(e) {
e.preventDefault();
webAuth.authorize();
});

logoutBtn.addEventListener(‘click’, logout);

function localLogin(authResult) {
// Set isLoggedIn flag in localStorage
localStorage.setItem(‘isLoggedIn’, ‘true’);
// Set the time that the access token will expire at
expiresAt = JSON.stringify(
authResult.expiresIn * 10 + new Date().getTime()
);
accessToken = authResult.accessToken;
idToken = authResult.idToken;
// alert(JSON.stringify(authResult));
}

function renewTokens() {
webAuth.checkSession({}, (err, authResult) => {
if (authResult && authResult.accessToken && authResult.idToken) {
localLogin(authResult);
} else if (err) {
alert(
'Could not get a new token ’ + err.error + ‘:’ + err.error_description + ‘.’
);
logout();
}
displayButtons();
});
}

function logout() {
// Remove isLoggedIn flag from localStorage
localStorage.removeItem(‘isLoggedIn’);
// Remove tokens and expiry time
accessToken = ‘’;
idToken = ‘’;
expiresAt = 0;
displayButtons();
}

function isAuthenticated() {
// Check whether the current time is past the
// access token’s expiry time
var expiration = parseInt(expiresAt) || 0;
return localStorage.getItem(‘isLoggedIn’) === ‘true’ && new Date().getTime() < expiration;
}
var yodata = ‘?X’;
function handleAuthentication()
{

 webAuth.parseHash(function(err, authResult) {
  if (authResult && authResult.accessToken && authResult.idToken) {
    window.location.hash = '';
    localLogin(authResult);
    loginBtn.style.display = 'none';
    homeView.style.display = 'inline-block';
  //  alert("Auth result " + JSON.stringify(authResult));
    
webAuth.client.userInfo(authResult.accessToken, function(err, user) {
    // This method will make a request to the /userinfo endpoint
    // and return the user object, which contains the user's information,
    // similar to the response below.
    alert(JSON.stringify(user));
   
   var idiv = document.getElementById('userinfo');
   idiv.innerHTML = JSON.stringify(user);       
   
      
          
});
        
  } else if (err) {
    homeView.style.display = 'inline-block';
    console.log(err);
    alert(
      'Error: ' + err.error + '. Check the console for further details.'
    );
  }
  displayButtons();
});

}

function displayButtons() {
if (isAuthenticated()) {
loginBtn.style.display = ‘none’;
logoutBtn.style.display = ‘inline-block’;
loginStatus.innerHTML = ‘You are logged in!’;
var idiv = document.getElementById(‘userinfo’);

// idiv.innerHTML = JSON.stringify(user);

} else {
  loginBtn.style.display = 'inline-block';
  logoutBtn.style.display = 'none';
  loginStatus.innerHTML = 'You are not logged in! Please log in to continue.';
   var idiv = document.getElementById('userinfo');
   idiv.innerHTML = "...."; 
    
}

}

if (localStorage.getItem(‘isLoggedIn’) === ‘true’) {
renewTokens();
} else {
handleAuthentication();
}
});