Advertisement
monstrasitix

Authentication

Sep 9th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | None | 0 0
  1. db = MySQLdb.connect("localhost", "root", "7May1996", "login_sys")
  2.  
  3.  
  4. # Check if username is valid
  5. # @param username {String} - Username to be checked for existance
  6. # @returns {Boolean} - If there are multiple table records
  7. # with matching username then user exists and function returns TRUE, otherwise FALSE.
  8. def usernameExists(username)
  9.     cursor = db.cursor()
  10.     cursor.execute('SELECT COUNT(*) FROM users WHERE user_name = %s', (username,))
  11.     username_match_count = cursor.fetchone()
  12.     db.close()
  13.     return username_match_count[0] > 0
  14.  
  15.  
  16. # Inserts user into a table
  17. # @param username {String} - Username
  18. # @param password {String} - Password
  19. def insertUser(username, password)
  20.     cursor = db.cursor()
  21.     cursor.execute('INSERT INTO users (user_name, user_password) VALUES ("%s", "%s")', (username, password))
  22.     db.commit()
  23.     db.close()
  24.    
  25.  
  26. # Getting an input value. Input can be anything and this needs to be validated
  27. # to suit a particular criteria. Unput must be 1 or 2.
  28. # Username and password can also be retrieved because they are both required down the line
  29. # for both options.
  30. user_choice = input("Test login system\noptions(press and enter)\n1. Login\n2. signup")
  31. input_username = input('Username: ')
  32. input_password = input('Password: ')
  33.    
  34. # Everything inside a try ... except block
  35. try:
  36.     # If choice is not 1 and not 2 then error out.
  37.     # You could build a looping mechanism here for retries
  38.     if user_choice != '1' or user_choice != '2':
  39.         raise Exception('Invalid choice')
  40.        
  41.     # Since we know that choice is 1 or 2 and both require username validation
  42.     # and database acces. We can preform this here
  43.     username_exists = usernameExists(input_username)
  44.    
  45.     # If logging in the username must exist. If it doesn't then we error or do something else
  46.     if user_choice == '1': # Login
  47.         if !username_exists:
  48.             raise Exception('Username does not exist')
  49.        
  50.         # Login Success
  51.    
  52.     # For signup we need to have a unique username. If it exists we can't use it.
  53.     elif user_choice == '2': # Signup
  54.         if username_exists:
  55.             raise Exception('Username is already taken')
  56.          
  57.         # Login Success
  58.         # We can also insert username with password
  59.         insertUser(input_username, input_password)
  60.  
  61. # All error handling for code under try can be preformed here        
  62. except Exception as error:
  63.     print(error.message)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement