Login simulation. Dictionary with [“username”]: [“password”] key:value pairs. How do I authenticate a user trying to log in?

Login simulation. Dictionary with [“username”]: [“password”] key:value pairs. How do I authenticate a user trying to log in?



Inside of a json file, each line stores the information of every different user that is created through separate classes. In the user_login file, retrieves this info and isolates username and passwords for each user to attempt to create a login page.


user_login



File: user_login


user_login


import json

filename = "users.json"
with open(filename, "r+", encoding='utf8') as file:
'''opens json file and separates it by line by storing each line into an
array'''
lines = file.readlines()

login_info =
'''array that will store usernames and passwords for each user(each line in
the file is a user)'''

for line in lines:
'''simply prints each element of the lines array displaying the
information of each user'''
info = json.loads(line)
print("USER: " + str(info))
print("username: " + info["username"])
print("password: " + info["password"] + "n")
login_info[info["username"]] = info["password"]
'''creates a new pair of username and password for each user(each line is
a user)'''
print(login_info)


print(lines)
print(login_info)

'''prompts user for their username and password'''
prompt_username = input("Please enter username: ")
prompt_password = input("Please input password: ")



The problem is in the following method(it does not work):


def login(username, password):
'''if username exists and the inputed strings match one of the key-value
pairs, login is successful'''

if username in login_info:
if password == info["password"]:
print("LOGIN SUCCESSFUL")
else:
print("Sorry, password does not exist.")
else:
print("Sorry this username or password does not exist.")

login(prompt_username, prompt_password)



How do I effectively check if the user inputs for username and password match any of the dictionary pairs to simulate a login?



The following is the users.json file.


users.json



File: users.json


users.json


"first": "Gilberto", "last": "Robles", "username": "girobles1", "password": "1234", "location": "San Diego", "interests": [["eat", "sleep", "code", "repeat"]]
"first": "Gilberto", "last": "Robles", "username": "girobles2", "password": "12345", "location": "San Diego", "interests": [["eat", "sleep", "code"]]





It's really hard to help when we don't have any examples of your data structures: login_info, lines/line
– Schalton
Sep 2 at 23:52





@Schalton I have included the file.
– Gilberto Robles Silva
Sep 3 at 1:22





Okay, thank you, my answer should suffice
– Schalton
Sep 3 at 1:27




1 Answer
1



It looks like you're just calling the wrong object to check if the password is correct


if password == info["password"]:



should be


if password == login_info[username]:



Follow-up question about number of attempts:



I rewrote your function to return the status of the username and password accuracy:


def login(username, password):
'''if username exists and the inputed strings match one of the key-value
pairs, login is successful returns (Username Correct, Password Correct)'''

if username in login_info:
if password == login_info[username]:
print("LOGIN SUCCESSFUL")
return (True, True)
else:
print("Sorry, password does not exist.")
return (True, False)
else:
print("Sorry this username does not exist.")
return (False, False)

login(prompt_username, prompt_password)



Then added a loop to check the results and logic to handle them (This is untested, since I don't have your dictionary):


'''prompts user for their username and password'''
username_attempts = 3
password_attempts = 3

prompt_username = input("Please enter username: ")
prompt_password = input("Please input password: ")

username_guess = 1
password_guess = 1

while True: #Loops until broken
if username_guess > username_attempts:
print("Too many invalid usernames, goodbye")
#What do you want it to do if they fail?
break

if password_guess > password_attempts:
print("Too many invalid passwords, goodbye")
#What do you want it to do if they fail?
break

username_status, password_status = login(prompt_username, prompt_password)

if all([username_status, password_status]):
break

elif not username_status:
print("Invalid username, please try again")
prompt_username = input("Please enter username: ")
username_guess += 1

elif not password_status:
print("Invalid password, please try again")
prompt_password = input("Please input password: ")
password_guess += 1





YES! This works thank you.
– Gilberto Robles Silva
Sep 3 at 1:42





One more thing, if I wanted to only allow a user to have restricted number of failed password attempts, how would I do that?
– Gilberto Robles Silva
Sep 3 at 1:54





Unless you print their status to a log that you read on open their attempt count would reset on close
– Schalton
Sep 3 at 1:57





You could similarly have built in the attempt loop into the login, but I wanted to show you how to make a function return values since it was unclear if you had/how you would progress after verifying the login
– Schalton
Sep 3 at 2:10





I solved it thank you.
– Gilberto Robles Silva
Sep 3 at 2:11



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)