Search code examples
pythonpython-3.xauthenticationtext-filesregistration

Python Login and Register System using text files


Hey I am trying to create a system using text files where a user can sign up and log in. All the data will be stored in plain text in a text file called User_Data.txt. My code works but I would like to know if there is anything I missed or If I could improve it in any way. Sorry for the Bad code Formatting in advance.

def choices():
    print("Please choose what you would like to do.")
    choice = int(input("For Sigining Up Type 1 and For Signing in Type 2: "))
    if choice == 1:
       return getdetails()
    elif choice == 2:
       return checkdetails()
    else:
       raise TypeError

def getdetails():
    print("Please Provide")
    name = str(input("Name: "))
    password = str(input("Password: "))
    f = open("User_Data.txt",'r')
    info = f.read()
    if name in info:
        return "Name Unavailable. Please Try Again"
    f.close()
    f = open("User_Data.txt",'w')
    info = info + " " +name + " " + password
    f.write(info)

def checkdetails():
    print("Please Provide")
    name = str(input("Name: "))
    password = str(input("Password: "))
    f = open("User_Data.txt",'r')
    info = f.read()
    info = info.split()
    if name in info:
        index = info.index(name) + 1
        usr_password = info[index]
        if usr_password == password:
            return "Welcome Back, " + name
        else:
            return "Password entered is wrong"
    else:
        return "Name not found. Please Sign Up."

print(choices())

Solution

  • There is a lot of improvements You could do. First of all, split functionality to smaller function.

    PASSWORD_FNAME = "User_Data.txt"
    
    def get_existing_users():
        with open("r", PASSWORD_FNAME ) as fp:
             for line in fp.readlines():
                 # This expects each line of a file to be (name, pass) seperated by whitespace
                 username, password = line.split()
                 yield username, password
    
    def is_authorized(username, password):
        return any((user == (username, password) for user in get_existing_users()) 
    
    def user_exists(username):
        return any((usr_name == username) for usr_name, _ in get_existing_users())
        # above is equivalent of:
        #
        # for usr_name, _ in get_existing_users():
        #     if usr_name == username:
        #        return True
        # return False
    
    def ask_user_credentials():
        print("Please Provide")
        name = str(input("Name: "))
        password = str(input("Password: "))
        return name, password
    
    def checkdetails():
        name, password = ask_user_credentials()
        if is_authorized(name, password):
           return "Welcome Back, " + name
        if user_exists(name):
           return "Password entered is wrong"
        return "Name not found. Please Sign Up."
    
    def getdetails():
        name, password = ask_user_credentials()
        if not user_exists(name):
           return "Name Unavailable. Please Try Again"
        # Not sure tho what would You like to do here
    

    It's always good to remember to always close your file if you read it. So if you do something like: f = open("r", "file.txt") remember to always call f.close() later. If you use context manager and do it like:

    with open("r", "file.txt") as fp:
         print(fp.read())
    

    it will automatically close the file for you at the end.