Encoding for Bcrypt in custom login tutorial

Good afternoon ladies and gents,

I am trying to replicate the custom sign-up flow tutorial and there is a specific server function that is not working.

Specifically:

@anvil.server.callable
def _do_signup(email, name, password):
  if name is None or name.strip() == "":
    return "Must supply a name"

  pwhash = bcrypt.hashpw(password, bcrypt.gensalt())

In the above last line where pwhash is being created, I get an error:

TypeError: Unicode-objects must be encoded before hashing at

I tried turning password into password.encode(‘utf-8’) or ‘latin-1’

but it then gives me a different error that is more cryptic:

anvil.server.ExecutionTerminatedError: Server code exited unexpectedly: 53c2d8dc17

Hey,

What you should have got is:

TypeError: Object of type 'bytes' is not JSON serializable

My guess is that you returned pwhash from the server function as raw bytes, rather than as a string, which Anvil doesn’t support. (If you want to send raw bytes around, put them into a BlobMedia. In this case, a bcrypt hash is an ASCII string anyway, so try just decode()ing it before returning it :slight_smile: )

I’m looking at our error reporting system now, to see why that error didn’t come out as you’d expect…

1 Like

likewise trying to implement the custom sign-up flow tutorial + some admin abilities like changing the password on an account, i started off with the administrative change password functionality and i was able to get this code to work for setting a password administratively:

user[‘password_hash’] = bcrypt.hashpw(password.encode(‘utf-8’), bcrypt.gensalt())

but after i make the change, when i try to log in with the new password, i get an InternalError: Internal server error: 7985e2c3953a

i have about 850 active users, will i have to move to the complete custom sign-up flow and force everyone to reset their passwords or does the custom sign-up flow tutorial use the same methodology as the Users service?

the only way to get an account working again after i set the password using the code above is to find it in the Users service and reset it there.

For anyone that stumbles on this looking for the answer, it’s answered over at Custom Signup Flow Error on Sign Up.

The working code is:

user[‘password_hash’] = bcrypt.hashpw(password.encode(), bcrypt.gensalt())

instead of:

user[‘password_hash’] = bcrypt.hashpw(password.encode(‘utf-8’), bcrypt.gensalt())
2 Likes