Home > database >  Why doesn't it switch to the else loop after the user logs in? - Flask, sessions, MySQL
Why doesn't it switch to the else loop after the user logs in? - Flask, sessions, MySQL

Time:03-20

I'm creating a bug management website, and I'm working on the login system. Everything is working, except it doesn't switch to the else statement once the user logs in (Even though session["loggedIn"] changes to true. I tried printing "Logged in" when the successful login loop triggers in login(), and it worked. It just doesn't switch.

[main.py]

from flask import *
import mysql.connector

db = mysql.connector.connect(host="localhost", user="root", passwd="kibafo33", database="PatchDB")
cursor = db.cursor()

app = Flask(__name__)
app.secret_key = "testkey"

reported_bugs = []
in_development = []
patched = []

with app.test_request_context('/'):

    session["loggedIn"] = False

    if not session["loggedIn"]:

        @app.route('/')
        def home():
            return render_template("notloggedin.html")

        @app.route('/signedup', methods=['GET', 'POST'])
        def signedup():
            username = request.form["usernameBox"]
            password = request.form["passwordBox"]
            cursor.execute("INSERT INTO PatchUser (username, password) VALUES (%s,%s)", (username, password))
            db.commit()
            return render_template("signedup.html")


        @app.route('/loggedin', methods=['GET', 'POST'])
        def loggedin():
            usernameinput = request.form["usernameBox"]
            passwordinput = request.form["passwordBox"]
            cursor.execute(f"SELECT password FROM PatchUser where username = '{usernameinput}'")
            for x in cursor:
                password_by_username = x[0]
            if passwordinput == password_by_username:
                print("User logged in.")
                session["loggedIn"] = True
                return render_template("loggedin.html")
            else:
                print("Wrong password!")
                return render_template("wrongpassword.html")

        @app.route('/login')
        def login():
            return render_template("login.html")

        @app.route('/signup')
        def signup():
            return render_template("signup.html")

    else:

        @app.route('/delete_bug/<bug>/<type>',methods = ['GET','POST'])
        def delete_bug(bug, type):
            if type == "reported_bugs":
                reported_bugs.remove(bug)
            elif type == "in_development":
                in_development.remove(bug)
            elif type == "patched":
                patched.remove(bug)
            return render_template("deleted.html")

        @app.route('/signedup', methods=['GET', 'POST'])
        def signedup():
            username = request.form["usernameBox"]
            password = request.form["passwordBox"]
            cursor.execute("INSERT INTO PatchUser (username, password) VALUES (%s,%s)", (username, password))
            db.commit()
            return render_template("signedup.html")

        @app.route('/loggedin', methods=['GET', 'POST'])
        def loggedin():
            usernameinput = request.form["usernameBox"]
            passwordinput = request.form["passwordBox"]
            cursor.execute(f"SELECT password FROM PatchUser where username = '{usernameinput}'")
            for x in cursor:
                password_by_username = x[0]
            if passwordinput == password_by_username:
                return render_template("loggedin.html")

        @app.route('/bugadded', methods=['GET', 'POST'])
        def bugadded():
            bugDesc = request.form["descriptionBox"]
            bugType = request.form["types"]
            print(bugType)
            if bugType == "reported":
                reported_bugs.append(bugDesc)
            elif bugType == "in-development":
                in_development.append(bugDesc)
            elif bugType == "patched":
                patched.append(bugDesc)
            return render_template("added.html")

        @app.route('/')
        def hello_world():
            return render_template("home.html", reported_bugs=reported_bugs, in_development=in_development, patched=patched)

        @app.route('/login')
        def login():
            return render_template("login.html")

        @app.route('/signup')
        def signup():
            return render_template("signup.html")

        @app.route('/addbug')
        def addbug():
            return render_template("addbug.html")

if __name__ == '__main__':
    app.run(debug=True)

Any help would be greatly appreciated!

CodePudding user response:

session["loggedIn"] = False
if not session["loggedIn"]

At the start of the program, that if statement is obviously false.

And this isn't inside a loop or a function, so it's only checked once.

What were you expecting to happen?

CodePudding user response:

Can you use a while loop that breaks once the user is logged in? Or do you want to keep checking? Not sure what your intentions are completely so this may not work for what youre trying to accomplish.

session["loggedIn"] = False
while session["loggedIn"] == False:
    (keep checking)
  • Related