Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions python-login-logout/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
def index():
if 'email' in session:
username = session['email']
return 'Logged in as ' + username + '<br>' + "<b><a href = '/logout'>click here to logout</a></b>"
return f"Logged in as {username} <br> <b><a href = '/logout'>click here to logout</a></b>"
return "You are not logged in <br><a href = '/login'></b>" + "click here to login</b></a>"

@app.route('/login')
Expand All @@ -17,34 +17,36 @@ def login():

@app.route('/submit', methods=['POST'])
def login_submit():
_email = request.form['inputEmail']
_password = request.form['inputPassword']
_email = request.form.get('inputEmail')
_password = request.form.get('inputPassword')
# validate the received values
if _email and _password and request.method == 'POST':
#check user exists
conn = mysql.connect()
cursor = conn.cursor()
sql = "SELECT * FROM tbl_user WHERE user_email=%s"
sql_where = (_email,)
cursor.execute(sql, sql_where)
row = cursor.fetchone()
if row:
if check_password_hash(row[3], _password):
session['email'] = row[1]
cursor.close()
conn.close()
return redirect('/')
else:
flash('Invalid password!')
return redirect('/login')
if not (_email and _password):
flash("Invalid email/password!")
return redirect("/login")
#check user exists
conn = mysql.connect()
cursor = conn.cursor()
sql = "SELECT * FROM tbl_user WHERE user_email=%s"
sql_where = (_email,)
cursor.execute(sql, sql_where)
row = cursor.fetchone()
if row:
if check_password_hash(row[3], _password):
session['email'] = row[1]
cursor.close()
conn.close()
return redirect('/')
else:
flash('Invalid email/password!')
flash('Invalid password!')
return redirect('/login')

else:
flash('Invalid email/password!')
return redirect('/login')

@app.route('/logout')
def logout():
session.pop('email', None)
return redirect('/')

if __name__ == "__main__":
app.run()
app.run()