Gagan0141 commited on
Commit
2caefcd
·
verified ·
1 Parent(s): 189d730

Upload 7 files

Browse files
Files changed (7) hide show
  1. app.py +152 -0
  2. readme.md +118 -0
  3. static/space-theme.css +61 -0
  4. templates/home.html +25 -0
  5. templates/login.html +36 -0
  6. templates/signup.html +32 -0
  7. users.db +0 -0
app.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ """
3
+ Flask Login System with SQLite
4
+ Features:
5
+ - Signup (create new account)
6
+ - Login (check user credentials)
7
+ - Session (to remember login state)
8
+ - Cookies (to store last visit info)
9
+ - "Remember Me" option (stay logged in even after closing browser)
10
+ """
11
+
12
+ from flask import Flask, render_template, request, redirect, url_for, session, make_response
13
+ import sqlite3
14
+ from datetime import timedelta
15
+
16
+ # Flask App Setup
17
+ app = Flask(__name__)
18
+
19
+ # Secret key is used to sign session data (must be kept secret in real apps!)
20
+ app.secret_key = "supersecretkey"
21
+
22
+ # Permanent sessions last for 7 days (used when "Remember Me" is checked)
23
+ app.permanent_session_lifetime = timedelta(days=7)
24
+
25
+
26
+ # Helper function to connect to SQLite database
27
+ def get_db_connection():
28
+ # Connect to SQLite database (creates file users.db if it doesn’t exist)
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
+ conn = get_db_connection()
37
+ conn.execute("""
38
+ CREATE TABLE IF NOT EXISTS users (
39
+ id INTEGER PRIMARY KEY AUTOINCREMENT, -- Auto-increment ID
40
+ username TEXT UNIQUE NOT NULL, -- Unique username
41
+ password TEXT NOT NULL -- Password (plain text for demo, should use hashing!)
42
+ )
43
+ """)
44
+ conn.commit()
45
+ conn.close()
46
+
47
+ # Call database initialization at startup
48
+ init_db()
49
+
50
+
51
+ # Home Page (only logged-in users can see this)
52
+ @app.route("/")
53
+ def home():
54
+ # Check if the user is logged in using session
55
+ if "username" in session:
56
+ username = session["username"] # Get logged-in username from session
57
+
58
+ # Get last visit message from cookie (if not found, show default message)
59
+ last_visit = request.cookies.get("last_visit", "First time visiting!")
60
+
61
+ return render_template("home.html", username=username, last_visit=last_visit)
62
+
63
+ # If not logged in, redirect to login page
64
+ return redirect(url_for("login"))
65
+
66
+
67
+ # Signup Page
68
+ @app.route("/signup", methods=["GET", "POST"])
69
+ def signup():
70
+ if request.method == "POST": # When user submits the form
71
+ username = request.form["username"]
72
+ password = request.form["password"]
73
+
74
+ conn = get_db_connection()
75
+ try:
76
+ # Insert new user into database
77
+ conn.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password))
78
+ conn.commit()
79
+ conn.close()
80
+
81
+ # After signup, redirect to login page
82
+ return redirect(url_for("login"))
83
+
84
+ except sqlite3.IntegrityError:
85
+ # This happens if the username already exists
86
+ return "Username already exists! Try another."
87
+
88
+ # If GET request, show signup form
89
+ return render_template("signup.html")
90
+
91
+
92
+ # Login Page
93
+ @app.route("/login", methods=["GET", "POST"])
94
+ def login():
95
+ if request.method == "POST": # When user submits login form
96
+ username = request.form["username"]
97
+ password = request.form["password"]
98
+
99
+ # Checkbox value: will be "on" if user ticks "Remember Me"
100
+ remember = request.form.get("remember")
101
+
102
+ # Check if username & password exist in database
103
+ conn = get_db_connection()
104
+ user = conn.execute("SELECT * FROM users WHERE username=? AND password=?",
105
+ (username, password)).fetchone()
106
+ conn.close()
107
+
108
+ if user:
109
+ # ✅ User found → start session
110
+ if remember == "on":
111
+ # Session will survive browser close (7 days)
112
+ session.permanent = True
113
+ else:
114
+ # Session ends when browser closes
115
+ session.permanent = False
116
+
117
+ # Store username inside session
118
+ session["username"] = username
119
+
120
+ # Create response with cookie
121
+ resp = make_response(redirect(url_for("home")))
122
+
123
+ # Save a cookie with "last visit" info
124
+ # If "Remember Me" checked → cookie valid for 7 days
125
+ # Else → cookie lasts only until browser closes
126
+ resp.set_cookie("last_visit", "Welcome back, " + username,
127
+ max_age=(7*24*60*60 if remember == "on" else None))
128
+
129
+ return resp
130
+ else:
131
+ # If username or password is wrong
132
+ return "Invalid username or password. Try again."
133
+
134
+ # If GET request, show login form
135
+ return render_template("login.html")
136
+
137
+
138
+ # Logout Page
139
+ @app.route("/logout")
140
+ def logout():
141
+ # Remove username from session
142
+ session.pop("username", None)
143
+
144
+ # Also delete the "last_visit" cookie
145
+ resp = make_response(redirect(url_for("login")))
146
+ resp.set_cookie("last_visit", "", expires=0)
147
+ return resp
148
+
149
+
150
+ # Run the App
151
+ if __name__ == "__main__":
152
+ app.run(debug=True, host="0.0.0.0", port=5000)
readme.md ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 🔑 Flask Login System with SQLite
2
+
3
+ A **beginner-friendly** login & signup system using **Flask** and **SQLite**.
4
+ This project demonstrates:
5
+
6
+ - ✅ User Signup (create a new account)
7
+ - ✅ User Login (check credentials)
8
+ - ✅ Session Management (keep users logged in)
9
+ - ✅ Cookies (store "last visit" info)
10
+ - ✅ "Remember Me" option (stay logged in even after closing browser)
11
+
12
+ ---
13
+
14
+ ## 📂 Project Structure
15
+
16
+ ```
17
+
18
+ /project-folder
19
+ │── app.py # Main Flask application
20
+ │── users.db # SQLite database (auto-created)
21
+ │── templates/
22
+ │ ├── home.html # Home page (only for logged-in users)
23
+ │ ├── login.html # Login form
24
+ │ └── signup.html # Signup form
25
+
26
+ ````
27
+
28
+ ---
29
+
30
+ ## ⚡ Installation & Setup
31
+
32
+ ### 1️⃣ Clone or Download
33
+ ```bash
34
+ git clone https://github.com/yourusername/flask-login-system.git
35
+ cd flask-login-system
36
+ ````
37
+
38
+ ### 2️⃣ Create Virtual Environment (recommended)
39
+
40
+ ```bash
41
+ python -m venv venv
42
+ source venv/bin/activate # On Mac/Linux
43
+ venv\Scripts\activate # On Windows
44
+ ```
45
+
46
+ ### 3️⃣ Install Dependencies
47
+
48
+ ```bash
49
+ pip install flask
50
+ ```
51
+
52
+ ### 4️⃣ Run the App
53
+
54
+ ```bash
55
+ python app.py
56
+ ```
57
+
58
+ Flask will start at:
59
+ 👉 [http://127.0.0.1:5000/](http://127.0.0.1:5000/)
60
+
61
+ ---
62
+
63
+ ## 🖥️ Usage
64
+
65
+ 1. Open `http://127.0.0.1:5000/signup`
66
+
67
+ * Create a new account.
68
+
69
+ 2. Go to `http://127.0.0.1:5000/login`
70
+
71
+ * Enter username & password.
72
+ * Tick **Remember Me** if you want to stay logged in after closing browser.
73
+
74
+ 3. After login → you’ll be redirected to **Home Page**.
75
+
76
+ * It will also show your **last visit info** stored in cookies.
77
+
78
+ 4. To logout → go to `http://127.0.0.1:5000/logout`.
79
+
80
+ ---
81
+
82
+ ## 🍪 How It Works
83
+
84
+ * **Sessions**
85
+
86
+ * Store who is logged in (`session["username"]`).
87
+ * By default, session ends when browser is closed.
88
+ * If "Remember Me" is checked → session lasts **7 days**.
89
+
90
+ * **Cookies**
91
+
92
+ * Store a `last_visit` message.
93
+ * If "Remember Me" is checked → cookie also lasts **7 days**.
94
+ * Otherwise, cookie disappears when browser closes.
95
+
96
+ ---
97
+
98
+ ## ⚠️ Security Notes (for teaching only!)
99
+
100
+ * Passwords are stored in **plain text** in this demo (❌ not secure).
101
+ * In real applications → **always hash passwords** using libraries like `werkzeug.security` or `bcrypt`.
102
+
103
+ ---
104
+
105
+ ## ✨ Future Improvements
106
+
107
+ * Add **password hashing** for security
108
+ * Add **email verification / password reset**
109
+ * Use **Flask-Login** for better authentication management
110
+
111
+ ---
112
+
113
+ ## 👨‍💻 Author
114
+
115
+ Created as a **teaching project for extreme beginners** learning Flask, Sessions, and Cookies.
116
+
117
+
118
+ ---
static/space-theme.css ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ body {
2
+ margin: 0;
3
+ padding: 0;
4
+ min-height: 100vh;
5
+ font-family: 'Segoe UI', Arial, sans-serif;
6
+ background: #0b0c2a;
7
+ color: #fff;
8
+ position: relative;
9
+ overflow: hidden;
10
+ }
11
+ .stars {
12
+ position: absolute;
13
+ width: 100%;
14
+ height: 100%;
15
+ top: 0; left: 0;
16
+ z-index: 0;
17
+ }
18
+ .star {
19
+ position: absolute;
20
+ background: white;
21
+ border-radius: 50%;
22
+ opacity: 0.8;
23
+ animation: twinkle 2s infinite alternate;
24
+ }
25
+ @keyframes twinkle {
26
+ from { opacity: 0.5; }
27
+ to { opacity: 1; }
28
+ }
29
+ .container {
30
+ position: relative;
31
+ z-index: 1;
32
+ max-width: 400px;
33
+ margin: 100px auto;
34
+ background: rgba(20, 24, 60, 0.85);
35
+ border-radius: 16px;
36
+ box-shadow: 0 0 30px #1a1a40;
37
+ padding: 40px 30px;
38
+ text-align: center;
39
+ }
40
+ h2 {
41
+ margin-bottom: 20px;
42
+ font-size: 2em;
43
+ letter-spacing: 2px;
44
+ }
45
+ p {
46
+ margin-bottom: 30px;
47
+ font-size: 1.1em;
48
+ }
49
+ a {
50
+ display: inline-block;
51
+ padding: 10px 30px;
52
+ background: #4e54c8;
53
+ color: #fff;
54
+ border-radius: 25px;
55
+ text-decoration: none;
56
+ font-weight: bold;
57
+ transition: background 0.3s;
58
+ }
59
+ a:hover {
60
+ background: #8f94fb;
61
+ }
templates/home.html ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Home</title>
5
+ <link rel="stylesheet" href="{{ url_for('static', filename='space-theme.css') }}">
6
+ </head>
7
+ <body>
8
+ <div class="stars">
9
+ <div class="star" style="top: 10%; left: 20%; width: 2px; height: 2px;"></div>
10
+ <div class="star" style="top: 30%; left: 70%; width: 3px; height: 3px;"></div>
11
+ <div class="star" style="top: 60%; left: 40%; width: 1.5px; height: 1.5px;"></div>
12
+ <div class="star" style="top: 80%; left: 80%; width: 2.5px; height: 2.5px;"></div>
13
+ <div class="star" style="top: 50%; left: 10%; width: 1px; height: 1px;"></div>
14
+ <div class="star" style="top: 20%; left: 90%; width: 2px; height: 2px;"></div>
15
+ <div class="star" style="top: 75%; left: 55%; width: 1.5px; height: 1.5px;"></div>
16
+ <div class="star" style="top: 40%; left: 60%; width: 2.5px; height: 2.5px;"></div>
17
+ </div>
18
+ <div class="container">
19
+ <h2>Welcome, {{ username }}!</h2>
20
+ <p>Last visit info (from cookie): {{ last_visit }}</p>
21
+ <a href="https://gagan0141.github.io/portfolio/">My Portfolio</a>
22
+ <a href="{{ url_for('logout') }}">Logout</a>
23
+ </div>
24
+ </body>
25
+ </html>
templates/login.html ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Login</title>
5
+ <link rel="stylesheet" href="{{ url_for('static', filename='space-theme.css') }}">
6
+ </head>
7
+ <body>
8
+ <div class="stars">
9
+ <div class="star" style="top: 10%; left: 20%; width: 2px; height: 2px;"></div>
10
+ <div class="star" style="top: 30%; left: 70%; width: 3px; height: 3px;"></div>
11
+ <div class="star" style="top: 60%; left: 40%; width: 1.5px; height: 1.5px;"></div>
12
+ <div class="star" style="top: 80%; left: 80%; width: 2.5px; height: 2.5px;"></div>
13
+ <div class="star" style="top: 50%; left: 10%; width: 1px; height: 1px;"></div>
14
+ <div class="star" style="top: 20%; left: 90%; width: 2px; height: 2px;"></div>
15
+ <div class="star" style="top: 75%; left: 55%; width: 1.5px; height: 1.5px;"></div>
16
+ <div class="star" style="top: 40%; left: 60%; width: 2.5px; height: 2.5px;"></div>
17
+ </div>
18
+ <div class="container">
19
+ <h2>Login</h2>
20
+ <form method="POST">
21
+ <label>Username:</label>
22
+ <input type="text" name="username" required><br><br>
23
+
24
+ <label>Password:</label>
25
+ <input type="password" name="password" required><br><br>
26
+
27
+ <label>
28
+ <input type="checkbox" name="remember"> Remember Me
29
+ </label><br><br>
30
+
31
+ <button type="submit">Login</button>
32
+ </form>
33
+ <p>Don't have an account? <a href="{{ url_for('signup') }}">Signup here</a></p>
34
+ </div>
35
+ </body>
36
+ </html>
templates/signup.html ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Signup</title>
5
+ <link rel="stylesheet" href="{{ url_for('static', filename='space-theme.css') }}">
6
+ </head>
7
+ <body>
8
+ <div class="stars">
9
+ <div class="star" style="top: 10%; left: 20%; width: 2px; height: 2px;"></div>
10
+ <div class="star" style="top: 30%; left: 70%; width: 3px; height: 3px;"></div>
11
+ <div class="star" style="top: 60%; left: 40%; width: 1.5px; height: 1.5px;"></div>
12
+ <div class="star" style="top: 80%; left: 80%; width: 2.5px; height: 2.5px;"></div>
13
+ <div class="star" style="top: 50%; left: 10%; width: 1px; height: 1px;"></div>
14
+ <div class="star" style="top: 20%; left: 90%; width: 2px; height: 2px;"></div>
15
+ <div class="star" style="top: 75%; left: 55%; width: 1.5px; height: 1.5px;"></div>
16
+ <div class="star" style="top: 40%; left: 60%; width: 2.5px; height: 2.5px;"></div>
17
+ </div>
18
+ <div class="container">
19
+ <h2>Signup</h2>
20
+ <form method="POST">
21
+ <label>Username:</label>
22
+ <input type="text" name="username" required><br><br>
23
+
24
+ <label>Password:</label>
25
+ <input type="password" name="password" required><br><br>
26
+
27
+ <button type="submit">Signup</button>
28
+ </form>
29
+ <p>Already have an account? <a href="{{ url_for('login') }}">Login here</a></p>
30
+ </div>
31
+ </body>
32
+ </html>
users.db ADDED
Binary file (16.4 kB). View file