Retrieve a value from MongoDB and set a string to it in Python(Pymongo) -
i trying retrieve value mongodb , set string in python mongodb. here user registration function:
def registeraccount(): registerusername = input('please input username registered our database.') registerpassword = input('please input password entered our database.') passwordhash = sha256_crypt.hash(registerpassword) regdetails = { "username": registerusername, "passwordhash": passwordhash } db = client.pinnacle users = db.users users.insert(regdetails)
and here login function:
def login(): db = client.pinnacle users = db.users pwhash = users.find_one({"passwordhash"}) print(str(pwhash)) loginusername = input('please input username signed in.') loginpassword = input('please input password signed in.') # pprint.pprint(users.find_one({"username": "1"})) # example = users.find_one({"username": "1"}) pbkdf2_sha256.verify(loginpassword, pwhash)
basically, need search database username, , string right after username passwordhash. set pwhash string checked passlib. appreciated. thanks! if interested can see full code here.
this happens whenever select login, after not typing in username or password
the error caused line below:
pwhash = users.find_one({"passwordhash"})
where should specify filter of key/value pair, instead it's specifying key. should remove line above (and print
line right after it). see pymongo find_one() method more info.
also, in same function login()
, have typo key username
.
user = users.find_one({"username": loginusername})
note should lowercase username
consistent how you're storing them elsewhere in code.
lastly, should replace
pbkdf2_sha256.verify(loginpassword, user['passwordhash'])
with below method, how encrypt in registeraccount()
sha256_crypt.verify(loginpassword, user['passwordhash'])
please consider adding error checks throughout code. i.e. if users.find_one()
returns none
xyz
.
Comments
Post a Comment