Rahul23232 commited on
Commit
fea3f29
ยท
verified ยท
1 Parent(s): 0543884

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +379 -2
README.md CHANGED
@@ -1,11 +1,388 @@
1
  ---
2
  title: Flask Login
3
- emoji: ๐Ÿจ
4
  colorFrom: green
5
  colorTo: indigo
6
  sdk: docker
7
- pinned: false
8
  license: mit
 
9
  ---
10
 
11
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  title: Flask Login
3
+ emoji: ๐Ÿ’ป
4
  colorFrom: green
5
  colorTo: indigo
6
  sdk: docker
7
+ pinned: true
8
  license: mit
9
+ short_description: A simple yet comprehensive Flask-based user authentication.
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
13
+
14
+ ---
15
+
16
+ # Flask Login System ๐Ÿ”
17
+
18
+ A simple yet comprehensive Flask-based user authentication system with SQLite database integration, featuring user registration, login, session management, and "Remember Me" functionality.
19
+
20
+ **Live Demo:** [https://rahul23232-flask-login.hf.space/](https://rahul23232-flask-login.hf.space/)
21
+
22
+ ## ๐Ÿ“‹ Table of Contents
23
+
24
+ - [Features](#features)
25
+ - [Technologies Used](#technologies-used)
26
+ - [Installation](#installation)
27
+ - [Usage](#usage)
28
+ - [Project Structure](#project-structure)
29
+ - [API Routes](#api-routes)
30
+ - [Database Schema](#database-schema)
31
+ - [Security Considerations](#security-considerations)
32
+ - [Contributing](#contributing)
33
+ - [License](#license)
34
+
35
+ ## โœจ Features
36
+
37
+ - **User Registration**: Create new user accounts with unique usernames
38
+ - **User Authentication**: Secure login system with credential verification
39
+ - **Session Management**: Server-side session handling for user state
40
+ - **Remember Me**: Optional persistent login that survives browser restarts
41
+ - **Cookie Management**: Last visit tracking with customizable expiration
42
+ - **SQLite Integration**: Lightweight database for user storage
43
+ - **Responsive Design**: Clean, user-friendly interface
44
+ - **Error Handling**: Proper validation and error messages
45
+
46
+ ## ๐Ÿ› ๏ธ Technologies Used
47
+
48
+ - **Backend**: Python 3.x, Flask
49
+ - **Database**: SQLite3
50
+ - **Frontend**: HTML, CSS (Bootstrap-compatible)
51
+ - **Session Management**: Flask Sessions
52
+ - **Deployment**: Hugging Face Spaces
53
+
54
+ ## ๐Ÿ“ฆ Installation
55
+
56
+ ### Prerequisites
57
+
58
+ - Python 3.7 or higher
59
+ - pip (Python package installer)
60
+
61
+ ### Local Setup
62
+
63
+ 1. **Clone the repository**
64
+ ```bash
65
+ git clone https://github.com/yourusername/flask-login-system.git
66
+ cd flask-login-system
67
+ ```
68
+
69
+ 2. **Install dependencies**
70
+ ```bash
71
+ pip install flask
72
+ ```
73
+
74
+ 3. **Run the application**
75
+ ```bash
76
+ python app.py
77
+ ```
78
+
79
+ 4. **Access the application**
80
+ - Open your browser and navigate to `http://localhost:5000`
81
+
82
+ ### Docker Setup (for Hugging Face Spaces)
83
+
84
+ The project includes Docker configuration for deployment on Hugging Face Spaces:
85
+
86
+ ```dockerfile
87
+ FROM python:3.9-slim
88
+
89
+ WORKDIR /app
90
+
91
+ COPY requirements.txt .
92
+ RUN pip install -r requirements.txt
93
+
94
+ COPY . .
95
+
96
+ EXPOSE 7860
97
+
98
+ CMD ["python", "app.py"]
99
+ ```
100
+
101
+ ## ๐ŸŽฏ Usage
102
+
103
+ ### Getting Started
104
+
105
+ 1. **Access the Application**: Navigate to the home URL
106
+ 2. **Create Account**: Click "Sign Up" to create a new user account
107
+ 3. **Login**: Use your credentials to log in
108
+ 4. **Remember Me**: Check the "Remember Me" option to stay logged in
109
+ 5. **Logout**: Click "Logout" to end your session
110
+
111
+ ### User Flow
112
+
113
+ ```mermaid
114
+ graph TD
115
+ A[Visit Homepage] --> B{User Logged In?}
116
+ B -->|No| C[Redirect to Login]
117
+ B -->|Yes| D[Show Dashboard]
118
+ C --> E[Login Form]
119
+ E --> F[Submit Credentials]
120
+ F --> G{Valid Credentials?}
121
+ G -->|No| H[Show Error]
122
+ G -->|Yes| I[Create Session]
123
+ I --> J{Remember Me?}
124
+ J -->|Yes| K[Set Permanent Session]
125
+ J -->|No| L[Set Temporary Session]
126
+ K --> D
127
+ L --> D
128
+ D --> M[Logout Option]
129
+ M --> N[Clear Session & Cookies]
130
+ N --> C
131
+ ```
132
+
133
+ ## ๐Ÿ“ Project Structure
134
+
135
+ ```
136
+ flask-login-system/
137
+ โ”œโ”€โ”€ app.py # Main Flask application
138
+ โ”œโ”€โ”€ templates/ # HTML templates
139
+ โ”‚ โ”œโ”€โ”€ base.html # Base template
140
+ โ”‚ โ”œโ”€โ”€ login.html # Login page
141
+ โ”‚ โ”œโ”€โ”€ signup.html # Registration page
142
+ โ”‚ โ””โ”€โ”€ home.html # Dashboard/home page
143
+ โ”œโ”€โ”€ static/ # Static files (CSS, JS, images)
144
+ โ”‚ โ””โ”€โ”€ style.css # Custom styles
145
+ โ”œโ”€โ”€ users.db # SQLite database (created automatically)
146
+ โ”œโ”€โ”€ requirements.txt # Python dependencies
147
+ โ”œโ”€โ”€ Dockerfile # Docker configuration
148
+ โ”œโ”€โ”€ README.md # Project documentation
149
+ โ””โ”€โ”€ .gitignore # Git ignore rules
150
+ ```
151
+
152
+ ## ๐Ÿ›ฃ๏ธ API Routes
153
+
154
+ ### Public Routes
155
+
156
+ | Route | Method | Description | Parameters |
157
+ |-------|---------|-------------|------------|
158
+ | `/login` | GET, POST | User login page | `username`, `password`, `remember` |
159
+ | `/signup` | GET, POST | User registration | `username`, `password` |
160
+
161
+ ### Protected Routes
162
+
163
+ | Route | Method | Description | Authentication Required |
164
+ |-------|---------|-------------|------------------------|
165
+ | `/` | GET | Homepage/Dashboard | Yes |
166
+ | `/logout` | GET | User logout | Yes |
167
+
168
+ ### Route Details
169
+
170
+ #### `/` (Homepage)
171
+ - **Method**: GET
172
+ - **Authentication**: Required
173
+ - **Description**: Main dashboard showing welcome message and last visit info
174
+ - **Response**: Redirects to login if not authenticated
175
+
176
+ #### `/signup` (Registration)
177
+ - **Methods**: GET, POST
178
+ - **Parameters**:
179
+ - `username` (string): Unique username
180
+ - `password` (string): User password
181
+ - **Validation**: Username must be unique
182
+ - **Response**: Redirects to login on success
183
+
184
+ #### `/login` (Authentication)
185
+ - **Methods**: GET, POST
186
+ - **Parameters**:
187
+ - `username` (string): User's username
188
+ - `password` (string): User's password
189
+ - `remember` (checkbox): Optional "Remember Me"
190
+ - **Response**: Redirects to homepage on success
191
+
192
+ #### `/logout` (Session Termination)
193
+ - **Method**: GET
194
+ - **Description**: Clears session and cookies
195
+ - **Response**: Redirects to login page
196
+
197
+ ## ๐Ÿ—„๏ธ Database Schema
198
+
199
+ ### Users Table
200
+
201
+ ```sql
202
+ CREATE TABLE users (
203
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
204
+ username TEXT UNIQUE NOT NULL,
205
+ password TEXT NOT NULL
206
+ );
207
+ ```
208
+
209
+ | Column | Type | Constraints | Description |
210
+ |--------|------|-------------|-------------|
211
+ | `id` | INTEGER | PRIMARY KEY, AUTOINCREMENT | Unique user identifier |
212
+ | `username` | TEXT | UNIQUE, NOT NULL | User's login name |
213
+ | `password` | TEXT | NOT NULL | User's password (plain text) |
214
+
215
+ ### Database Operations
216
+
217
+ - **Connection**: SQLite3 with `sqlite3.Row` factory for dictionary-like access
218
+ - **Initialization**: Automatic table creation on first run
219
+ - **Queries**: Parameterized queries to prevent SQL injection
220
+
221
+ ## ๐Ÿ”’ Security Considerations
222
+
223
+ ### Current Implementation
224
+
225
+ - โœ… SQL injection protection via parameterized queries
226
+ - โœ… Session-based authentication
227
+ - โœ… CSRF protection through Flask's secret key
228
+ - โœ… Input validation and error handling
229
+
230
+ ### Production Recommendations
231
+
232
+ - โš ๏ธ **Password Hashing**: Currently stores plain text passwords
233
+ ```python
234
+ from werkzeug.security import generate_password_hash, check_password_hash
235
+
236
+ # For registration
237
+ hashed_password = generate_password_hash(password)
238
+
239
+ # For login verification
240
+ check_password_hash(stored_hash, provided_password)
241
+ ```
242
+
243
+ - โš ๏ธ **Environment Variables**: Move secret key to environment variables
244
+ ```python
245
+ import os
246
+ app.secret_key = os.environ.get('SECRET_KEY', 'fallback-secret-key')
247
+ ```
248
+
249
+ - โš ๏ธ **HTTPS**: Enable HTTPS in production
250
+ - โš ๏ธ **Rate Limiting**: Implement login attempt limits
251
+ - โš ๏ธ **Input Validation**: Add comprehensive input sanitization
252
+ - โš ๏ธ **Session Security**: Configure secure session cookies
253
+
254
+ ### Recommended Security Enhancements
255
+
256
+ ```python
257
+ # Enhanced security configuration
258
+ app.config.update(
259
+ SESSION_COOKIE_SECURE=True, # HTTPS only
260
+ SESSION_COOKIE_HTTPONLY=True, # No JS access
261
+ SESSION_COOKIE_SAMESITE='Lax', # CSRF protection
262
+ PERMANENT_SESSION_LIFETIME=timedelta(hours=1) # Shorter sessions
263
+ )
264
+ ```
265
+
266
+ ## ๐ŸŽจ Frontend Templates
267
+
268
+ ### Template Structure
269
+
270
+ All templates extend `base.html` for consistent styling:
271
+
272
+ ```html
273
+ <!-- base.html -->
274
+ <!DOCTYPE html>
275
+ <html lang="en">
276
+ <head>
277
+ <meta charset="UTF-8">
278
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
279
+ <title>{% block title %}Flask Login System{% endblock %}</title>
280
+ <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
281
+ </head>
282
+ <body>
283
+ <div class="container mt-4">
284
+ {% block content %}{% endblock %}
285
+ </div>
286
+ </body>
287
+ </html>
288
+ ```
289
+
290
+ ### Form Examples
291
+
292
+ #### Login Form
293
+ ```html
294
+ <form method="POST">
295
+ <div class="mb-3">
296
+ <label for="username" class="form-label">Username</label>
297
+ <input type="text" class="form-control" name="username" required>
298
+ </div>
299
+ <div class="mb-3">
300
+ <label for="password" class="form-label">Password</label>
301
+ <input type="password" class="form-control" name="password" required>
302
+ </div>
303
+ <div class="mb-3 form-check">
304
+ <input type="checkbox" class="form-check-input" name="remember">
305
+ <label class="form-check-label">Remember Me</label>
306
+ </div>
307
+ <button type="submit" class="btn btn-primary">Login</button>
308
+ </form>
309
+ ```
310
+
311
+ ## ๐Ÿš€ Deployment
312
+
313
+ ### Hugging Face Spaces
314
+
315
+ The project is configured for deployment on Hugging Face Spaces:
316
+
317
+ 1. **Create Space**: Create a new Space on Hugging Face
318
+ 2. **Upload Files**: Push your code to the Space repository
319
+ 3. **Configuration**: Ensure `app.py` runs on port 7860
320
+ 4. **Environment**: The Space will automatically build and deploy
321
+
322
+ ### Local Development
323
+
324
+ ```bash
325
+ # Development mode with auto-reload
326
+ export FLASK_ENV=development
327
+ export FLASK_DEBUG=1
328
+ python app.py
329
+ ```
330
+
331
+ ### Production Deployment
332
+
333
+ ```bash
334
+ # Use a production WSGI server
335
+ pip install gunicorn
336
+ gunicorn -w 4 -b 0.0.0.0:5000 app:app
337
+ ```
338
+
339
+ ## ๐Ÿค Contributing
340
+
341
+ 1. Fork the repository
342
+ 2. Create a feature branch (`git checkout -b feature/amazing-feature`)
343
+ 3. Commit your changes (`git commit -m 'Add amazing feature'`)
344
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
345
+ 5. Open a Pull Request
346
+
347
+ ### Development Guidelines
348
+
349
+ - Follow PEP 8 style guidelines
350
+ - Add comments for complex logic
351
+ - Update documentation for new features
352
+ - Test all functionality before submitting
353
+
354
+ ## ๐Ÿ“ License
355
+
356
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
357
+
358
+ ## ๐Ÿ› Known Issues
359
+
360
+ - Plain text password storage (security concern)
361
+ - No password strength validation
362
+ - No account recovery mechanism
363
+ - Limited input sanitization
364
+
365
+ ## ๐Ÿ”ฎ Future Enhancements
366
+
367
+ - [ ] Password hashing with bcrypt/scrypt
368
+ - [ ] Email verification system
369
+ - [ ] Password reset functionality
370
+ - [ ] User profile management
371
+ - [ ] Role-based access control
372
+ - [ ] OAuth integration (Google, GitHub)
373
+ - [ ] Rate limiting and brute force protection
374
+ - [ ] Advanced session management
375
+ - [ ] API endpoints for mobile apps
376
+ - [ ] Unit and integration tests
377
+
378
+ ## ๐Ÿ“ž Support
379
+
380
+ For questions, issues, or contributions:
381
+
382
+ - **Repository**: [GitHub Repository](https://github.com/yourusername/flask-login-system)
383
+ - **Live Demo**: [Flask Login System](https://rahul23232-flask-login.hf.space/)
384
+ - **Issues**: Use GitHub Issues for bug reports and feature requests
385
+
386
+ ---
387
+
388
+ **Note**: This is a demonstration project for educational purposes. For production use, implement proper security measures including password hashing, HTTPS, and comprehensive input validation.