# Buggy code
def account_check(username, password, request):
    parent_id = username
    try:
        existing = request.registry.storage.get(
            parent_id=parent_id, collection_id="account", object_id=username
        )
    except storage_exceptions.RecordNotFoundError:
        return None

    hashed = existing["password"]
    pwd_str = password.encode(encoding="utf-8")
    if hashed == bcrypt.hashpw(pwd_str, hashed):
        return True  # Match! Return anything but None.


# Traceback (TypeError)
Unicode-objects must be encoded before hashing
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/pyramid/tweens.py", line 22, in excview_tween
response = handler(request)
File "/usr/local/lib/python3.5/dist-packages/pyramid_tm/__init__.py", line 119, in tm_tween
reraise(*exc_info)
File "/usr/local/lib/python3.5/dist-packages/pyramid_tm/compat.py", line 15, in reraise
raise value
File "/usr/local/lib/python3.5/dist-packages/pyramid_tm/__init__.py", line 98, in tm_tween
response = handler(request)
File "/usr/local/lib/python3.5/dist-packages/pyramid/router.py", line 155, in handle_request
view_name
File "/usr/local/lib/python3.5/dist-packages/pyramid/view.py", line 612, in _call_view
response = view_callable(context, request)
File "/usr/local/lib/python3.5/dist-packages/pyramid/config/views.py", line 181, in __call__
return view(context, request)
File "/usr/local/lib/python3.5/dist-packages/pyramid/viewderivers.py", line 389, in attr_view
return view(context, request)
File "/usr/local/lib/python3.5/dist-packages/pyramid/viewderivers.py", line 367, in predicate_wrapper
return view(context, request)
File "/usr/local/lib/python3.5/dist-packages/pyramid/viewderivers.py", line 438, in rendered_view
result = view(context, request)
File "/usr/local/lib/python3.5/dist-packages/pyramid/viewderivers.py", line 147, in _requestonly_view
response = view(request)
File "/usr/local/lib/python3.5/dist-packages/cornice/service.py", line 493, in wrapper
response = view_(request)
File "/code/kinto/core/views/hello.py", line 45, in get_hello
if Authenticated in request.effective_principals:
File "/usr/local/lib/python3.5/dist-packages/pyramid/security.py", line 375, in effective_principals
return policy.effective_principals(self)
File "/usr/local/lib/python3.5/dist-packages/pyramid_multiauth/__init__.py", line 119, in effective_principals
userid = policy.authenticated_userid(request)
File "/usr/local/lib/python3.5/dist-packages/pyramid/authentication.py", line 92, in authenticated_userid
callback_ok = self.callback(userid, request)
File "/usr/local/lib/python3.5/dist-packages/pyramid/authentication.py", line 1123, in callback
return self.check(username, password, request)
File "/code/kinto/plugins/accounts/authentication.py", line 18, in account_check
if hashed == bcrypt.hashpw(pwd_str, hashed):
File "/usr/local/lib/python3.5/dist-packages/bcrypt/__init__.py", line 62, in hashpw
raise TypeError("Unicode-objects must be encoded before hashing")

# Fixed code
def account_check(username, password, request):
    parent_id = username
    try:
        existing = request.registry.storage.get(
            parent_id=parent_id, collection_id="account", object_id=username
        )
    except storage_exceptions.RecordNotFoundError:
        return None

    hashed = existing["password"].encode(encoding="utf-8")
    pwd_str = password.encode(encoding="utf-8")
    if hashed == bcrypt.hashpw(pwd_str, hashed):
        return True  # Match! Return anything but None.