Created
June 5, 2021 11:20
-
-
Save shinysu/a9cb43f52418c13f270873447c795b6a to your computer and use it in GitHub Desktop.
login3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, render_template, redirect, url_for, request, session | |
from db_functions import get_password, insert_user | |
app = Flask(__name__) | |
app.secret_key = "123@abc" | |
def set_session(email): #{"logged_in": True, "email"='[email protected]'} | |
session["logged_in"] = True | |
session["email"] = email | |
def verify_login(email, password): | |
stored_password = get_password(email) | |
if len(stored_password): #[('1111,)] | |
if password == stored_password[0][0]: | |
set_session(email) | |
return redirect("/") | |
else: | |
print("wrong password") | |
return redirect("/login") | |
else: | |
insert_user(email, password) | |
set_session(email) | |
return redirect("/") | |
@app.route("/login", methods=["GET", "POST"]) | |
def login(): | |
if request.method == "GET": | |
return render_template('login.html') | |
elif request.method == "POST": | |
email = request.form["email"] | |
password = request.form["password"] | |
verify_login(email, password) | |
return redirect(url_for('index')) | |
@app.route("/") | |
def index(): | |
if "logged_in" in session: | |
return render_template('welcome.html', email=session["email"]) | |
else: | |
return redirect(url_for('login')) | |
if __name__ == '__main__': | |
app.run(debug=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Welcome</title> | |
</head> | |
<body> | |
<h1> Welcome {{email}} </h1> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment