I'm trying to make an authentication system on by backend server based on FastAPI.
But I'm currently facing an issue with bcrypt when I try to login the user, the hashed password sent from the user in the post call is never equal to the one stored in the database.
This is the code I use to sign up the user:
@app.put('/users')
def createUser(user: createUser):
salt = bcrypt.gensalt()
hashed = bcrypt.hashpw(user.password.encode('utf-8'), salt)
if dbUser.find_one({"username":user.username}) is None:
user.password = hashed.decode('utf-8')
print(user)
dbUser.insert_one(bson.son.SON(user))
return {"message":"success"}
else:
return {"error":"user already exists!"}
when it comes to saving the new user.password. hashed.decode('utf-8') helps me saving a normal string in the database instead of " u'hashed_string' " which I get by doing str(hashed) or Binary("hashed_string", 0) which I get by just saving user.password as hashed withtout str or decode.
The code I use for the login is:
@app.post('/users')
def loginUser(user: loginUser):
if dbUser.find_one({"username":user.username}) is None:
return {"error":"user not found"}
else:
salt = bcrypt.gensalt()
hashed = bcrypt.hashpw(user.password.encode('utf-8'), salt)
dbPassword = dbUser.find_one({"username":user.username})['password'].encode('utf-8')
if bcrypt.checkpw(dbPassword, hashed):
return {"message":"success!"}
else:
print("does not match")
return {"message":"wrong password!"}
I am comparing the dbPassowrd encoded, which since it was a string, it will became u'hashed_password' and the hashed and encoded password sent in the post call, which will be u'hashed_password' The issue is that the password sent by the user, once crypted and encoded, is not equal to the one in the database. What am I doing wrong?