Search code examples
pythonsqlsqliteauthenticationtwisted

Python authentication problem with SQLite


I have a Database in python with 2 fields: Username and passHash. The DB is ok, i already checked and all the parameters are correct. Then i need an authentication system that will look each row for the username, if it finds, look the 2nd field for the password, if not the same as user-provided, return "pass incorrect". but the problem is: how can i do it so the system will loop through all the rows and when it finishes, it will return "User doesn't exists".. Because now it returns user not found in the first row searched, sounds freaking noob but let's go D:

Ps. using twisted and sqlite3

    def authenticate(self, username, password):
    playerDB.execute('''SELECT * FROM playerData''')
    for row in playerDB:
        if row[0] == username:
            if row[1] == password:
                if username in ADMIN_NAMES:
                    self.server.sendOutput("Admin authentication: %s" % username)
                    logging.info("Admin authentication: %s" % username)
                return "Authenticated"
            else:
                logging.info("Authentication Fail: %s" % username)
                return "Password doesn't matches username."

        else:
            return "This player doesn't exists."

Solution

  • Just move your second else clause out to the for loop instead of the outer if statement. If you get to the end of the loop without an error, break, or return, it will be executed.