Created
June 5, 2021 11:41
-
-
Save shinysu/b0bab0df2a934b7e9c45b830e8629a34 to your computer and use it in GitHub Desktop.
login_complete
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 datetime import timedelta | |
from db_functions import get_password, insert_user | |
app = Flask(__name__) | |
app.secret_key = "123@abc" | |
app.permanent_session_lifetime = timedelta(minutes=5) | |
def set_session(email): #{"logged_in": True, "email"='[email protected]'} | |
session.permanent = True | |
session["logged_in"] = True | |
session["email"] = email | |
def reset_session(): | |
if "logged_in" in session: | |
del session["logged_in"] | |
del session["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("/") | |
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("/logout") | |
def logout(): | |
reset_session() | |
return redirect("/") | |
@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
import sqlite3 | |
sql_query = """ | |
CREATE TABLE IF NOT EXISTS users( | |
email TEXT PRIMARY KEY, | |
password TEXT NOT NULL | |
); | |
""" | |
def execute_query(sql_query): | |
with sqlite3.connect("login.db") as conn: | |
cur = conn.cursor() | |
result = cur.execute(sql_query) | |
conn.commit() | |
return result | |
if __name__ == '__main__': | |
execute_query(sql_query) |
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
import sqlite3 | |
def execute_query(sql_query): | |
with sqlite3.connect("login.db") as conn: | |
cur = conn.cursor() | |
result = cur.execute(sql_query) | |
conn.commit() | |
return result | |
def insert_user(email, password): | |
sql_query = "INSERT INTO users(email, password) VALUES('%s', '%s') " % (email, password) | |
execute_query(sql_query) | |
def get_password(email): | |
sql_query = "SELECT password FROM users WHERE email='%s' " % (email) | |
password = execute_query(sql_query).fetchall() | |
return password | |
if __name__ == '__main__': | |
#insert_user('[email protected]', '1111') | |
get_password('[email protected]') |
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
body{ | |
font-size: 1.5em; | |
} | |
div{ | |
margin: 5vh; | |
} | |
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>login</title> | |
<link rel="stylesheet" href="{{url_for('static', filename='css/login.css')}}" | |
</head> | |
<body> | |
<form action="#" method="POST"> | |
<div> | |
<label for="email"> Enter your email id: </label> | |
<input type="email" name="email"> | |
</div> | |
<div> | |
<label for="password"> Enter your password: </label> | |
<input type="password" name="password"> | |
</div> | |
<div> | |
<button type="submit" name="submit">Login</button> | |
</div> | |
</form> | |
</body> | |
</html> |
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