Gagan0141 commited on
Commit
2cc60b4
·
verified ·
1 Parent(s): dc3ac14

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +152 -0
  2. templates/home.html +25 -0
  3. templates/login.html +36 -0
  4. templates/signup.html +32 -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)
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>