'How to create login function for different user groups with Flask Session
I'm trying to set up a login function for each user type with Python using Flask Session. For example, there are two user types: employees and supervisors and I want to show different pages when each user type logs in.
I have a csv file (userinfo) that contains the user information including user name and password.
username password
employee employee
supervisor supervisor
I currently have the following block of code to achieve it, but it seems like it's not working. If the employee user logs in, I want to show the employee page and if the supervisor group logs in, I want to show the supervisor page. But it seems like the current code takes the users to the same homepage.
userinfo = pd.read_csv("userinfo.csv")
@app.route("/homepage", methods=["GET", "POST"])
def login():
# get user name and password input from a html form.
username = request.form["username"]
password = request.form['password']
# get user name and password from the csv file.
get_user = userinfo[userinfo["user_name"] == username].to_dict("records")[0]
get_password = userinfo[userinfo["password"] == password].to_dict("records")[0]
# login function
if username and password == get_password:
session["user_name"] = username
session["password"] = password
print("Login Session:", session)
if username == "employee":
return redirect("/employee_page")
elif username == "supervisor":
return redirect("/supervisor_page")
else:
return redirect("/homepage")
else:
return redirect("/homepage")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
