Search code examples
javascriptpythonfidoyubikey

python fido2 server with if statement


I'm using the this example as inspiration for my project about Yubikey. At the end of authenticate.html, I want to add an if-statement. So if the authentication is successfull redirect to a specific page, and otherwise recirect back to the homepage. I've tried different places for the if-statement:

.then(function(response) {
  var stat = response.ok ? 'successful' : 'unsuccessful';
  alert('Authentication ' + stat + ' More details in server log...');
}, function(reason) {
  alert(reason);
}).then(
  function() {
    if(stat=='successful'){
      window.location.replace('https://google.com')
    }
    else {
      window.location = '/';
    }
});
}

and

.then(function(response) {
  var stat = response.ok ? 'successful' : 'unsuccessful';
  alert('Authentication ' + stat + ' More details in server log...');
  if(stat=='successful'){
      window.location.replace('https://google.com')
    }
    else {
      window.location = '/';
    }
}, function(reason) {
  alert(reason);
}).then(
  function() {
});
}

I've never seen Javascript before, but it seemed that I couldn't get around Yubikey and Python (I'm familiar with python) not using JS. None of the above has worked the intented ways.


Solution

  • Asuming you're using fetch then the correct (and simplest) way of handling this would be something like the following:

    fetch('some-super-secret-endpoint')
    .then(response => {
        if(response.ok)
            window.location = 'https://google.com'
        else 
            window.location = '/'
    }).catch(e => {
        console.error(e)
    })