Authentication problem

What I’m trying to do:
I’m trying to authenticate my users with anvil.users.login_with_email
What I’ve tried and what’s not working:
I currently have 3 users (gmwebman, nickb, martinc) with their passwords the same as their usernames for testing. If on my login form I provide correct credentials the user is successfully logged in. If I supply an incorrect password I get a TypeError instead of AuthenticationFailed : TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types
at frmLogin, line 43

Code Sample:

# this is a formatted code snippet.
  def btnLogin_click(self, **event_args):
    pass
    if self.txtUsername.text == '':
      alert('Please enter your username')
    elif self.txtPassword.text == '':
      alert('Please enter your password')
    else:
      uu = app_tables.users.search()
      un = ''
      em = ''
      for u in uu:
        if u['username'] == self.txtUsername.text:
          un = u['username']
          em = u['email']
      if un == '':
        alert('Login failed. No such username.')
      else:
        pw = self.txtPassword.text
        try:
43      user = anvil.users.login_with_email(em, pw)
          if user['isadmin']:
            alert('Administrator has logged in.')
          else:
            alert('User has logged in.')
        except 'AuthenticationFailed':
          alert('Login failed. Incorrect password.')
# paste your code between ``` 

Clone link:

share a copy of your app

could it simply be that the line needs to be indented within the try block

Thanks for that dsweet. I eventually spotted the indentation error myself and corrected it but the TypeError: isinstance() is still occuring.

That’s pretty strange.

When I cloned your app and played with it, I regularly saw the error you’re seeing when I entered in a username with a garbage password. If I entered the correct password, the user logged in successfully.

I created a new app to test with, and in that one entering the wrong password gets the expected “wrong password” error message, not the error you’re seeing.

Yes, that’s exactly the behaviour I’m seeing. Weird.

I created a new app with nothing but a custom login form. Initially I did not put the “anvil.users.login_with_email” in a try block. A correct username and password produced a successful login, an incorrect password produced the expected “AuthenticationFailed” exception.
I then put the login in a try block with an except block to catch the above:
except ‘AuthenticationFailed’:
As before a correct username+password produced a successful login but an incorrect password produced the weird TypeError: isinstance() exception. The weirdness only appears when the login is in a try!

the error message isn’t so nice

it should be more like:

TypeError: catching classes that do not inherit from BaseException is not allowed

because you have

        except 'AuthenticationFailed':

which is not a valid python exception type

this should fix your issue

        except anvil.users.AuthenticationFailed:
2 Likes

Thanks again stucork!

I assume that the try block was working perfectly but the invalid exception type created an uncaught exception which was the weird one I was seeing.

Yeah. If you run the following code in Python locally you’ll get a similar, but more complete error traceback.

try:
  raise Exception("foo")
except "Exception":
  pass

for each except block python will check if the error is an instance of that line.