Two months ago I started learning python, but not regularly, and today I made my first mini "authenticator", just because I wanted to understand how JSON works.
I used AI to debug some things, because I started with an authenticator without json saves, then I found out about JSON(and combining them was difficult), but most of it was typed by me. Take a look at my code and share your thoughts on how I used JSON and conditionals.
P.S. I know somethings are pretty basic or not the most secure, but as I said, it's just to understand how JSON and conditionals work in this kind of program.
# Welcome to Authenticator
import sys, json, os
file = "users.json"
## LOAD FILE
def loadAcc():
`try:`
`with open(file, "r") as f:`
`users = json.load(f)`
`except json.JSONDecodeError:`
`users = {}`
`return users`
## SAVE CHANGES FUNCTION
def saveChang(users, file="users.json"):
`with open(file + ".tmp", "w") as f:`
`json.dump(users, f, indent=4)`
`os.replace(file + ".tmp", file)`
## SIGNUP FUNCTION - NO JSON
def signup(users, name):
`for k, v in users[name].items():`
`credential = input(f"Type your {k}: ")`
`users[name][k] = credential`
`saveChang(users, file)`
`return`
## LOGIN FUNCTION - NO JSON
def login(users, name):
`count = 0`
`while count < 3:`
`user_input = input("Enter your username: ")`
`pass_input = input("Enter your password: ")`
`if user_input == users[name]["Username"] and pass_input == users[name]["Password"]:`
`print("\nLogin Succesfully")`
`return "enter"`
`count += 1`
`print("Something went wrong")`
`print("You blocked your account")`
`return changeAccount(users, name)`
## CHANGE ACCOUNT FUNCTION - NO JSON
def changeAccount(users, name):
`set_qt = input("""What you wanna change?`
`1. Username`
`2. Password`
`3. Delete Account`
`4. Exit`
`""").lower()`
`if set_qt in ["1", "username"]:`
`ch_qt = input("Enter your password: ")`
`if ch_qt == users[name]["Password"]:`
`new = input("Enter your username: ")`
`new_check = input("Enter your username again: ")`
`if new == new_check:`
users[name]["Username"] = new
saveChang(users)
print("Change was succesfullly :)")
return True
`else:`
print("Something went wrong :(")
changeAccount(users, name)
`else:`
`print("Something went wrong :(")`
`changeAccount(users, name)`
`elif set_qt in ["2", "password"]:`
`ch_qt = input("Enter your username: ")`
`if ch_qt == users[name]["Username"]:`
`new = input("Enter your password: ")`
`new_check = input("Enter your password again: ")`
`if new == new_check:`
users[name]["Password"] = new
saveChang(users)
print("Change was succesfullly :)")
return True
`else:`
print("Something went wrong :(")
changeAccount(users, name)
`else:`
`print("Something went wrong :(")`
`changeAccount(users, name)`
`elif set_qt in ["3", "delete account"]:`
`final_qt = input("Are you sure?(yes or not): ").lower()`
`if final_qt == "yes":`
`del users[name]`
`saveChang(users)`
`print("I hope see you later :(")`
`return False`
`elif final_qt == "not":`
`print("Well, that\'s good :)")`
`return True`
`else:`
`print("Something went wrong")`
`changeAccount(users, name)`
`elif set_qt in ["exit", "4"]:`
`return True`
`else:`
`print("Something went wrong")`
`changeAccount(users, name)`
## INTERFACE
def interface():
global users
while True:
name = input("What's your name?: ")
if name not in users:
users[name] = {"Username": "", "Password": ""}
registrado = users[name]["Username"] and users[name]["Password"]
if registrado:
opciones = ["login", "change account", "exit"]
else:
opciones = ["signup", "login", "exit"]
print("\nDo you want to...")
for i, op in enumerate(opciones, 1):
print(f"{i}. {op.title()}")
qt = input("---> ").lower()
# acepta el número o el nombre
if qt.isdigit() and 1 <= int(qt) <= len(opciones):
qt = opciones[int(qt) - 1]
if qt == "signup":
signup(users, name)
elif qt == "login":
if login(users, name) == "enter":
break
elif qt == "change account":
if changeAccount(users, name) == False:
break
elif qt == "exit":
sys.exit()
else:
print("Interface: ERROR")
if os.path.exists(file):
`users = loadAcc()`
else:
`users = {}`
interface()