Delete app.py
Browse files
app.py
DELETED
|
@@ -1,134 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Flask Login System with SQLite (Secure Version)
|
| 3 |
-
Features:
|
| 4 |
-
- Signup (with password hashing)
|
| 5 |
-
- Login (with secure password check)
|
| 6 |
-
- Session (login state management)
|
| 7 |
-
- Cookies (store last visit info securely)
|
| 8 |
-
- "Remember Me" option
|
| 9 |
-
- Flash messages for better UX
|
| 10 |
-
"""
|
| 11 |
-
|
| 12 |
-
from flask import Flask, render_template, request, redirect, url_for, session, make_response, flash
|
| 13 |
-
import sqlite3
|
| 14 |
-
from datetime import timedelta
|
| 15 |
-
from werkzeug.security import generate_password_hash, check_password_hash
|
| 16 |
-
|
| 17 |
-
# Flask App Setup
|
| 18 |
-
app = Flask(__name__)
|
| 19 |
-
|
| 20 |
-
# Secret key is used to sign session data (must be kept secret in real apps!)
|
| 21 |
-
app.secret_key = "supersecretkey"
|
| 22 |
-
|
| 23 |
-
# Permanent sessions last for 7 days (used when "Remember Me" is checked)
|
| 24 |
-
app.permanent_session_lifetime = timedelta(days=7)
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
# Helper function to connect to SQLite database
|
| 28 |
-
def get_db_connection():
|
| 29 |
-
conn = sqlite3.connect("users.db")
|
| 30 |
-
conn.row_factory = sqlite3.Row # Makes rows behave like dictionaries
|
| 31 |
-
return conn
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
# Initialize database with a "users" table
|
| 35 |
-
def init_db():
|
| 36 |
-
with get_db_connection() as conn:
|
| 37 |
-
conn.execute("""
|
| 38 |
-
CREATE TABLE IF NOT EXISTS users (
|
| 39 |
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 40 |
-
username TEXT UNIQUE NOT NULL,
|
| 41 |
-
password TEXT NOT NULL
|
| 42 |
-
)
|
| 43 |
-
""")
|
| 44 |
-
conn.commit()
|
| 45 |
-
|
| 46 |
-
# Call database initialization at startup
|
| 47 |
-
init_db()
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
# Home Page (only logged-in users can see this)
|
| 51 |
-
@app.route("/")
|
| 52 |
-
def home():
|
| 53 |
-
if "username" in session:
|
| 54 |
-
username = session["username"]
|
| 55 |
-
last_visit = request.cookies.get("last_visit", "First time visiting!")
|
| 56 |
-
return render_template("home.html", username=username, last_visit=last_visit)
|
| 57 |
-
|
| 58 |
-
return redirect(url_for("login"))
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
# Signup Page
|
| 62 |
-
@app.route("/signup", methods=["GET", "POST"])
|
| 63 |
-
def signup():
|
| 64 |
-
if request.method == "POST":
|
| 65 |
-
username = request.form["username"]
|
| 66 |
-
password = request.form["password"]
|
| 67 |
-
|
| 68 |
-
hashed_password = generate_password_hash(password, method="sha256")
|
| 69 |
-
|
| 70 |
-
try:
|
| 71 |
-
with get_db_connection() as conn:
|
| 72 |
-
conn.execute(
|
| 73 |
-
"INSERT INTO users (username, password) VALUES (?, ?)",
|
| 74 |
-
(username, hashed_password)
|
| 75 |
-
)
|
| 76 |
-
conn.commit()
|
| 77 |
-
|
| 78 |
-
flash("Signup successful! Please login.", "success")
|
| 79 |
-
return redirect(url_for("login"))
|
| 80 |
-
|
| 81 |
-
except sqlite3.IntegrityError:
|
| 82 |
-
flash("Username already exists! Try another.", "danger")
|
| 83 |
-
return redirect(url_for("signup"))
|
| 84 |
-
|
| 85 |
-
return render_template("signup.html")
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
# Login Page
|
| 89 |
-
@app.route("/login", methods=["GET", "POST"])
|
| 90 |
-
def login():
|
| 91 |
-
if request.method == "POST":
|
| 92 |
-
username = request.form["username"]
|
| 93 |
-
password = request.form["password"]
|
| 94 |
-
remember = request.form.get("remember")
|
| 95 |
-
|
| 96 |
-
with get_db_connection() as conn:
|
| 97 |
-
user = conn.execute("SELECT * FROM users WHERE username=?", (username,)).fetchone()
|
| 98 |
-
|
| 99 |
-
if user and check_password_hash(user["password"], password):
|
| 100 |
-
session["username"] = username
|
| 101 |
-
session.permanent = True if remember == "on" else False
|
| 102 |
-
|
| 103 |
-
resp = make_response(redirect(url_for("home")))
|
| 104 |
-
resp.set_cookie(
|
| 105 |
-
"last_visit",
|
| 106 |
-
"Welcome back, " + username,
|
| 107 |
-
max_age=(7*24*60*60 if remember == "on" else None),
|
| 108 |
-
httponly=True,
|
| 109 |
-
secure=False, # ❗set True in production (requires HTTPS)
|
| 110 |
-
samesite="Lax"
|
| 111 |
-
)
|
| 112 |
-
|
| 113 |
-
flash("Login successful!", "success")
|
| 114 |
-
return resp
|
| 115 |
-
else:
|
| 116 |
-
flash("Invalid username or password.", "danger")
|
| 117 |
-
return redirect(url_for("login"))
|
| 118 |
-
|
| 119 |
-
return render_template("login.html")
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
# Logout Page
|
| 123 |
-
@app.route("/logout")
|
| 124 |
-
def logout():
|
| 125 |
-
session.pop("username", None)
|
| 126 |
-
resp = make_response(redirect(url_for("login")))
|
| 127 |
-
resp.set_cookie("last_visit", "", expires=0)
|
| 128 |
-
flash("You have been logged out.", "info")
|
| 129 |
-
return resp
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
# Run the App
|
| 133 |
-
if __name__ == "__main__":
|
| 134 |
-
app.run(debug=True, host="0.0.0.0", port=5000)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|