Advertisement
acclivity

pySimpleUserRegAndLogin

Jun 16th, 2021 (edited)
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.07 KB | None | 0 0
  1. # User Registration And Login Example
  2. # Mike Kerry - June 2021
  3.  
  4. # Note: In this simple example, actual passwords are stored in the user details file
  5. # In a real-world situation, actual passwords would not be stored, only password hashes
  6.  
  7. # Example of user_details text file:-
  8. # Mike,abc123
  9. # Fred,fredpass
  10.  
  11. userdict = {}       # This dictionary will hold User IDs and Passwords read from the user_details file and from user input
  12.  
  13. while True:
  14.     try:
  15.         fin = open("user_details.txt")
  16.     except:
  17.         break                   # user_detais.txt does not exist. Start a new user dictionary
  18.  
  19.     for uline in fin:               # Read 1 line of user-details
  20.         uline = uline.strip()       # Remove End-of-Line characters
  21.         usplit = uline.split(",")   # split line on comma
  22.         userid = usplit[0]          # Get the UserID
  23.         userpswd = usplit[1]        # Get the User Password
  24.         if userid in userdict:      # Check this User ID does not exist already
  25.             print("Duplicate user on file:", userid)
  26.         else:
  27.             userdict[userid] = userpswd     # Create a dictionary entry for this user
  28.     fin.close()
  29.     break
  30.  
  31. while True:                 # Main Menu Loop
  32.     while True:                 # Loop looking for valid menu option
  33.         print("\nMenu")
  34.         print("1: Register new user")
  35.         print("2. Log in existing user")
  36.         print("8. Show all users and passwords")            # (Should be an Administrator option only)
  37.         print("9: Quit")
  38.         option = input("Choose option: ")
  39.         if option in ["1", "2", "8", "9"]:
  40.             break
  41.         print("Invalid input")
  42.  
  43.     if option == "1":           # Register new user
  44.         while True:             # Keep looping until a valid new User ID is entered
  45.             userid = input("Enter user ID: ")
  46.             if len(userid) > 12:
  47.                 print("User ID must not be longer than 12 characters")
  48.                 continue
  49.             if userid not in userdict:              # Check User ID not already on the system
  50.                 break
  51.             print("Sorry, that user ID already exists\n")
  52.             # Loop around asking for a User ID
  53.  
  54.         while True:         # Keep looping until a valid password is chosen
  55.             userpswd = input("Choose a password: ")
  56.             if len(userpswd) > 5 and "," not in userpswd:
  57.                 break
  58.             print("Passwords must be 6 characters or more and may not include a comma")
  59.  
  60.         userdict[userid] = userpswd         # Add new user+password to the dictionary
  61.         print("Congratulations. User", userid, "is now registered")
  62.         print("You may now use option 2 to Log In to the system")
  63.         input("Press Enter to continue")
  64.  
  65.     elif option == "2":         # Log-in an existing user
  66.         userid = input("Enter user ID: ")
  67.         if userid in userdict:          # Check that the User ID exists in our dictionary
  68.             userpswd = input("Enter password: ")        # Request password
  69.             if userpswd == userdict[userid]:            # Check password matches the one held on file
  70.                 print("You have successfully logged in as", userid)
  71.                 #
  72.                 # HERE THE LOGGED IN USER CAN DO OTHER PROCESSES
  73.                 #
  74.                 input("Press Enter to continue")
  75.                 continue
  76.         print("Invalid user ID or password")
  77.  
  78.     elif option == "8":         # Print a list of User IDs and Passwords
  79.         # Obviously, in practice, this option would only be accessible by Admin
  80.         sortkeys = sorted(userdict.keys())
  81.         for userid in sortkeys:
  82.             print(userid.ljust(16), userdict[userid])
  83.  
  84.     elif option == "9":         # Quit. Write updated dictionary to text file
  85.         fout = open("user_details.txt", "w")
  86.         for userid in userdict:
  87.             # Write out UserID and Password separated by a comma, and end with New-Line
  88.             fout.write(userid + "," + userdict[userid] + "\n")
  89.         fout.close()
  90.         break                   # Break out of main menu loop, so that the program terminates
  91. print("Goodbye")
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement