Created
June 12, 2021 11:27
-
-
Save shinysu/a962b1b7d975073c803fb1c926985229 to your computer and use it in GitHub Desktop.
login_full_app
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, flash | |
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: | |
flash("You've got a 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() | |
flash("You've been logged out!!!") | |
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
<!DOCTYPE html> | |
<html lang="en"> | |
{% include "header.html" %} | |
<body> | |
<h1> Programming club </h1> | |
<div class="container"> | |
<nav class="navbar navbar-expand-lg navbar-light bg-light"> | |
<div class="container-fluid"> | |
<a class="navbar-brand" href="#">Navbar</a> | |
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> | |
<span class="navbar-toggler-icon"></span> | |
</button> | |
<div class="collapse navbar-collapse" id="navbarNav"> | |
<ul class="navbar-nav"> | |
<li class="nav-item"> | |
<a class="nav-link active" aria-current="page" href="#">Home</a> | |
</li> | |
<li class="nav-item"> | |
<a class="nav-link" href="#">Features</a> | |
</li> | |
<li class="nav-item"> | |
<a class="nav-link" href="#">Pricing</a> | |
</li> | |
<li class="nav-item"> | |
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a> | |
</li> | |
</ul> | |
</div> | |
</div> | |
</nav> | |
{% block content %}{% endblock %} | |
</div> | |
</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
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
<head> | |
<meta charset="UTF-8"> | |
<title>login</title> | |
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous"> | |
<link rel="stylesheet" href="{{url_for('static', filename='css/login.css')}}" | |
</head> |
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: 1em; | |
} | |
.error{ | |
margin-left: 5vh; | |
color: red; | |
} | |
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
{% extends "base.html" %} | |
{% block content %} | |
<form action="#" method="POST"> | |
<div class="row"> | |
<div class="col-md-2"> | |
<label for="email"> Enter your email id: </label> | |
</div> | |
<div class="col-md-4"> | |
<input class="form-control" type="email" name="email"> | |
</div> | |
</div> | |
<div class="row"> | |
<div class="col-md-2"> | |
<label for="password"> Enter your password: </label> | |
</div> | |
<div class="col-md-4"> | |
<input class="form-control" type="password" name="password"> | |
</div> | |
</div> | |
<div class="row"> | |
<div class="col-md-2"></div> | |
<div class="col-md-2"> | |
<button type="submit" name="submit">Login</button> | |
</div> | |
</div> | |
{% with messages = get_flashed_messages() %} | |
{% if messages %} | |
{% for message in messages %} | |
<div class="error"> | |
{{ message }} | |
</div> | |
{% endfor %} | |
{% endif %} | |
{% endwith %} | |
</form> | |
{% endblock %} |
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
{% extends "base.html" %} | |
{% block content %} | |
<h1> Welcome {{email}} </h1> | |
{% endblock %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment