0

~~This Question is a continuation of another question.~~ I'm creating a basic Login Form and I am using Flask for serving my HTML Pages and Sqlite3 Package in python for storage of usernames and passwords!

I started by creating an App Route!

I'm requesting the User Input from the form and then comparing it with the data I retrieve from my database named 'Users.db' as follows:

@app.route('/login', methods=["GET", "POST"])
def login():
    msg = ''
    
    if request.method == 'POST' and 'username' in request.form and 'password' in request.form:
        # Create variables for easy access
        username = request.form['username']
        password = request.form['password']

        #Connection Est.
        connection = sqlite3.connect("Users.db")

        #cursor
        USer_crsr = connection.cursor()

        user_cm = """SELECT username FROM accounts where"""
        user_cm_2 = """SELECT password FROM accounts"""

        USer_crsr.execute(user_cm)
        USer_crsr.execute(user_cm_2)

        USer_result = USer_crsr.fetchall()
        USer_result_2 = USer_crsr.fetchall()

        if username in USer_result and if password in USer_result_2:
            # Create session data, we can access this data in other routes
              session['loggedin'] = True
              session['id'] = USer_result['id']
              session['username'] = USer_result['username']
              # Redirect to home page
              return 'Logged in successfully!'
        else:
            # Account doesnt exist or username/password incorrect
            msg = 'Incorrect username/password!'
    return render_template("login.html", msg=msg)

(I know this code isn't correct!)

Any suggestions on what I can do to correctly retrieve my data from the database and comparing it to the User Input?

  • 2
    What if you used [flask-login](https://flask-login.readthedocs.io/en/latest/) instead of reinventing the wheel? This module is specially designed for authentication management – Tobin Dec 22 '21 at 11:17
  • I had tried using flask-login earlier but It isn't for advanced productional needs... – Alston V. Abraham Dec 22 '21 at 11:42

1 Answers1

0

So, after weeks of researching and reading articles, I finally got the article containing the answer to my Question and my Problem. So, I'm just gonna' post the article link here! (It is an Article-Series, so it is recommended that you follow the whole series for more information, though reading the article alone also worked for me!)

Also, please do read the Article wholly to understand all the components!

Here's the link: - https://medium.com/analytics-vidhya/flask-development-part-4-database-configuration-648b11f708a5

Article Series ||| Github Repository