I'm creating a little banking system. As for now, it's purely in terminal.
You can login by providing your username and password. I created a class to validate if given username and password matches data in my database.
It will ask for your username and password over and over if it won't match.
After that, a user will be created from User class, and so on...
The problem is:
When you type your credentials at first try, everything works fine.
But when you type wrong credentials, app will prompt for username and password again and login() function will return username, but it won't be assigned to a variable that will be used to create a user.
login() function:
from Login.loginValidator import loginValidator
def login():
validator = loginValidator()
username = input("Enter Username: ")
password = input("Enter Password: ")
result = validator.validateLogin(username, password)
if result:
print(username)
return username
else:
login()
loginValidator class:
class loginValidator:
def validateLogin(self, username, password):
def checkIfUsernameExists(username):
connection = psycopg2.connect(
"dbname={} user={} host={} password={}".format(dbname, dbuser, dbhost, dbpassword))
cursor = connection.cursor()
cursor.execute("SELECT username FROM clientstandardinfo WHERE username = %s", [username])
result = cursor.fetchone()
cursor.close()
connection.close()
gc.collect()
if result is not None:
return True
result = checkIfUsernameExists(username)
if result:
connection = psycopg2.connect(
"dbname={} user={} host={} password={}".format(dbname, dbuser, dbhost, dbpassword))
cursor = connection.cursor()
cursor.execute("SELECT password FROM clientstandardinfo WHERE username = %s", [username])
passResult = cursor.fetchone()[0]
if sha256_crypt.verify(password, passResult):
print("Login Success")
return True
else:
print("Wrong password")
return False
else:
print("Username doesn't exist")
return False
getUserInfo function:
def getUserInfo(clientUsername):
print("username from getUserInfo: \n", clientUsername)
connection = psycopg2.connect("dbname={} user={} host={} password={}".format(dbname, dbuser, dbhost, dbpassword))
cursor = connection.cursor()
cursor.execute("SELECT * FROM clientstandardinfo WHERE username = %s",[clientUsername])
result = cursor.fetchone()
print("Result from getUserInfo: \n",result)
cursor.close()
connection.close()
#1=firstname, 2=lastname, 3=username, 4=password,
# 5=email, 6=dateOfBirth, 7=gender, 8=admin, 9=dateofregister
return result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9]
main() function:
def main():
username = login()
print("MAIN FUNCTION - username:\n",username)
client_user = User(*getUserInfo(username))
Output is (after typing wrong credentials for the first time):
<prints correct username that comes from print in login() function>
MAIN FUNCTION - username:
None
username from getUserInfo:
None
Result from getUserInfo:
None
Traceback (most recent call last):
File "/Users/mac/Dropbox/banking raw/main.py", line 24, in <module>
main()
File "/Users/mac/Dropbox/banking raw/main.py", line 16, in main
client_user = User(*getUserInfo(username))
File "/Users/mac/Dropbox/banking raw/db.py", line 22, in getUserInfo
return result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9]
TypeError: 'NoneType' object is not subscriptable
Process finished with exit code 1
I don't quite get why it's not working. I hope you understand my explanation
I will appreciate your help. Also, any feedback about the 'quality' of code if highly welcome.