fasthtml-auth 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,413 @@
1
+ Metadata-Version: 2.4
2
+ Name: fasthtml-auth
3
+ Version: 0.1.0
4
+ Summary: Complete authentication system for FastHTML applications with beautiful UI, role-based access control, and session management
5
+ Author-email: John Richmond <confusedjohn46@gmail.com>
6
+ Maintainer-email: John Richmond <confusedjohn46@gmail.com>
7
+ License: MIT License
8
+
9
+ Copyright (c) 2024 John Richmond
10
+
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ of this software and associated documentation files (the "Software"), to deal
13
+ in the Software without restriction, including without limitation the rights
14
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ copies of the Software, and to permit persons to whom the Software is
16
+ furnished to do so, subject to the following conditions:
17
+
18
+ The above copyright notice and this permission notice shall be included in all
19
+ copies or substantial portions of the Software.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
+ SOFTWARE.
28
+ Project-URL: Homepage, https://github.com/fromLittleAcorns/fasthtml-auth
29
+ Project-URL: Documentation, https://github.com/fromLittleAcorns/fasthtml-auth#readme
30
+ Project-URL: Repository, https://github.com/fromLittleAcorns/fasthtml-toolkit
31
+ Project-URL: Issues, https://github.com/fromLittleAcorns/fasthtml-auth/issues
32
+ Project-URL: Changelog, https://github.com/fromLittleAcorns/fasthtml-auth/blob/main/CHANGELOG.md
33
+ Keywords: fasthtml,authentication,web,framework,auth,login,session,bcrypt,monsterui
34
+ Classifier: Development Status :: 4 - Beta
35
+ Classifier: Intended Audience :: Developers
36
+ Classifier: License :: OSI Approved :: MIT License
37
+ Classifier: Programming Language :: Python :: 3
38
+ Classifier: Programming Language :: Python :: 3.8
39
+ Classifier: Programming Language :: Python :: 3.9
40
+ Classifier: Programming Language :: Python :: 3.10
41
+ Classifier: Programming Language :: Python :: 3.11
42
+ Classifier: Programming Language :: Python :: 3.12
43
+ Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
44
+ Classifier: Topic :: Internet :: WWW/HTTP :: WSGI :: Application
45
+ Classifier: Topic :: Security
46
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
47
+ Classifier: Topic :: Software Development :: User Interfaces
48
+ Requires-Python: >=3.8
49
+ Description-Content-Type: text/markdown
50
+ License-File: LICENSE
51
+ Requires-Dist: python-fasthtml>=0.12.0
52
+ Requires-Dist: monsterui>=1.0.20
53
+ Requires-Dist: fastlite>=0.2.0
54
+ Requires-Dist: bcrypt>=4.0.0
55
+ Provides-Extra: dev
56
+ Requires-Dist: pytest>=7.0.0; extra == "dev"
57
+ Requires-Dist: pytest-asyncio>=0.21.0; extra == "dev"
58
+ Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
59
+ Requires-Dist: black>=23.0.0; extra == "dev"
60
+ Requires-Dist: flake8>=6.0.0; extra == "dev"
61
+ Requires-Dist: mypy>=1.0.0; extra == "dev"
62
+ Requires-Dist: pre-commit>=3.0.0; extra == "dev"
63
+ Provides-Extra: docs
64
+ Requires-Dist: mkdocs>=1.4.0; extra == "docs"
65
+ Requires-Dist: mkdocs-material>=8.5.0; extra == "docs"
66
+ Requires-Dist: mkdocs-autorefs>=0.4.0; extra == "docs"
67
+ Dynamic: license-file
68
+
69
+ # FastHTML-Auth
70
+
71
+ A comprehensive, drop-in authentication system for FastHTML applications with beautiful UI components, session management, and role-based access control.
72
+
73
+ This repro is intended to simplify the process of setting up a authentiation, user management and database setup for fastHTML apps. Some of the code is based upon examples from Answer.ai code base, and for more information about fastHTML see [fastHTML](https://www.fastht.ml)
74
+
75
+ ## Installation
76
+
77
+ ```bash
78
+ pip install fasthtml-auth
79
+
80
+ ## Features
81
+
82
+ - 🔐 **Secure Authentication** - Bcrypt password hashing, session management
83
+ - 👤 **User Management** - Registration, profile management, role-based access
84
+ - 🎨 **Beautiful UI** - Styled with MonsterUI components, fully responsive
85
+ - 🛡️ **Role-Based Access Control** - User, Manager, Admin roles with decorators
86
+ - 🔄 **Session Management** - Automatic session handling with FastHTML
87
+ - 📱 **Mobile Friendly** - Responsive design works on all devices
88
+ - ⚡ **Easy Integration** - Drop-in solution with minimal setup required
89
+
90
+ ## Quick Start
91
+
92
+ ### Installation
93
+
94
+ ```bash
95
+ pip install fasthtml-auth
96
+ ```
97
+
98
+ ### Quick Start
99
+
100
+ ```python
101
+ from fasthtml.common import *
102
+ from monsterui.all import *
103
+ from fasthtml_auth import AuthManager
104
+
105
+ # Initialize authentication system
106
+ auth = AuthManager(
107
+ db_path="data/app.db",
108
+ config={
109
+ 'allow_registration': True,
110
+ 'public_paths': ['/about', '/contact']
111
+ }
112
+ )
113
+
114
+ # Set up database and middleware
115
+ db = auth.initialize()
116
+ beforeware = auth.create_beforeware()
117
+
118
+ # Create FastHTML app with authentication
119
+ app = FastHTML(
120
+ before=beforeware,
121
+ secret_key='your-secret-key-change-in-production',
122
+ hdrs=Theme.blue.headers() # MonsterUI styling
123
+ )
124
+
125
+ # Register authentication routes
126
+ auth.register_routes(app)
127
+
128
+ # Your protected routes
129
+ @app.route("/")
130
+ def dashboard(req):
131
+ user = req.scope['user'] # Automatically available
132
+ return Title("Dashboard"), H1(f"Welcome, {user.username}!")
133
+
134
+ @app.route("/admin")
135
+ @auth.require_admin()
136
+ def admin_panel(req):
137
+ return Title("Admin"), H1("Admin Only Area")
138
+
139
+ if __name__ == "__main__":
140
+ serve(port=5000)
141
+ ```
142
+
143
+ That's it! Your app now has:
144
+ - Login/logout at `/auth/login` and `/auth/logout`
145
+ - User registration at `/auth/register`
146
+ - Profile management at `/auth/profile`
147
+ - Role-based access control
148
+ - Beautiful, responsive forms
149
+
150
+ ## Configuration
151
+
152
+ ```python
153
+ config = {
154
+ 'login_path': '/auth/login', # Custom login URL
155
+ 'public_paths': ['/about', '/api'], # Routes that don't require auth
156
+ 'allow_registration': True, # Enable user registration
157
+ 'allow_password_reset': False, # Enable password reset (coming soon)
158
+ }
159
+
160
+ auth = AuthManager(db_path="data/app.db", config=config)
161
+ ```
162
+
163
+ ## User Roles and Access Control
164
+
165
+ ### Available Roles
166
+ - **user** - Basic authenticated user
167
+ - **manager** - Manager privileges + user access
168
+ - **admin** - Full system access
169
+
170
+ ### Role-Based Route Protection
171
+
172
+ ```python
173
+ # Require specific roles
174
+ @app.route("/manager-area")
175
+ @auth.require_role('manager', 'admin')
176
+ def manager_view(req, *args, **kwargs):
177
+ return H1("Manager+ Only")
178
+
179
+ # Admin only shortcut
180
+ @app.route("/admin")
181
+ @auth.require_admin()
182
+ def admin_panel(req, *args, **kwargs):
183
+ return H1("Admin Only")
184
+
185
+ # Check roles in templates
186
+ @app.route("/dashboard")
187
+ def dashboard(req):
188
+ user = req.scope['user']
189
+
190
+ admin_link = A("Admin Panel", href="/admin") if user.role == 'admin' else None
191
+ manager_link = A("Manager Area", href="/manager") if user.role in ['manager', 'admin'] else None
192
+
193
+ return Div(admin_link, manager_link)
194
+ ```
195
+
196
+ ## User Object
197
+
198
+ In protected routes, `req.scope['user']` contains:
199
+
200
+ ```python
201
+ user.id # Unique user ID
202
+ user.username # Username
203
+ user.email # Email address
204
+ user.role # 'user', 'manager', or 'admin'
205
+ user.active # Boolean - account status
206
+ user.created_at # Account creation timestamp
207
+ user.last_login # Last login timestamp
208
+ ```
209
+
210
+ ## Database Schema
211
+
212
+ FastHTML-Auth automatically creates these tables:
213
+
214
+ ```sql
215
+ -- Users table
216
+ CREATE TABLE user (
217
+ id INTEGER PRIMARY KEY,
218
+ username TEXT UNIQUE NOT NULL,
219
+ email TEXT UNIQUE NOT NULL,
220
+ password TEXT NOT NULL, -- Bcrypt hashed
221
+ role TEXT DEFAULT 'user',
222
+ created_at TEXT,
223
+ last_login TEXT,
224
+ active INTEGER DEFAULT 1
225
+ );
226
+
227
+ -- Sessions table (for future use)
228
+ CREATE TABLE session (
229
+ id TEXT PRIMARY KEY,
230
+ user_id INTEGER,
231
+ data TEXT,
232
+ expires_at TEXT,
233
+ created_at TEXT
234
+ );
235
+ ```
236
+
237
+ ## Styling and Themes
238
+
239
+ FastHTML-Auth uses [MonsterUI](https://github.com/pixeltable/monster-ui) for beautiful, consistent styling.
240
+
241
+ ```python
242
+ # Choose your theme
243
+ app = FastHTML(
244
+ before=beforeware,
245
+ hdrs=Theme.blue.headers() # or Theme.red, Theme.green, etc.
246
+ )
247
+ ```
248
+
249
+ All forms include:
250
+ - Responsive card-based layouts
251
+ - Professional input styling
252
+ - Clear error/success messages
253
+ - Loading states and validation
254
+ - Mobile-optimized design
255
+
256
+ ## API Reference
257
+
258
+ ### AuthManager
259
+
260
+ ```python
261
+ class AuthManager:
262
+ def __init__(self, db_path="data/app.db", config=None)
263
+ def initialize(self) -> Database
264
+ def create_beforeware(self, additional_public_paths=None) -> Beforeware
265
+ def register_routes(self, app, prefix="/auth") -> Dict
266
+ def require_role(self, *roles) -> Decorator
267
+ def require_admin(self) -> Decorator
268
+ def get_user(self, username: str) -> Optional[User]
269
+ ```
270
+
271
+ ### Default Admin Account
272
+
273
+ A default admin account is created automatically:
274
+ - **Username**: `admin`
275
+ - **Password**: `admin123`
276
+ - **Role**: `admin`
277
+
278
+ ⚠️ **Change this password in production!**
279
+
280
+ ## Available Routes
281
+
282
+ FastHTML-Auth automatically registers these routes:
283
+
284
+ - `GET /auth/login` - Login form
285
+ - `POST /auth/login` - Login submission
286
+ - `GET /auth/logout` - Logout and redirect
287
+ - `GET /auth/register` - Registration form (if enabled)
288
+ - `POST /auth/register` - Registration submission (if enabled)
289
+ - `GET /auth/profile` - User profile management
290
+ - `POST /auth/profile` - Profile update submission
291
+
292
+ ## Examples
293
+
294
+ ### Complete Example App
295
+
296
+ ```python
297
+ from fasthtml.common import *
298
+ from monsterui.all import *
299
+ from fasthtml_auth import AuthManager
300
+
301
+ # Initialize auth
302
+ auth = AuthManager(
303
+ db_path="data/myapp.db",
304
+ config={
305
+ 'allow_registration': True,
306
+ 'public_paths': ['/about', '/pricing', '/contact']
307
+ }
308
+ )
309
+
310
+ db = auth.initialize()
311
+ beforeware = auth.create_beforeware()
312
+
313
+ app = FastHTML(
314
+ before=beforeware,
315
+ secret_key='super-secret-change-me',
316
+ hdrs=Theme.blue.headers()
317
+ )
318
+
319
+ auth.register_routes(app)
320
+
321
+ # Public landing page
322
+ @app.route("/")
323
+ def home(req):
324
+ user = req.scope.get('user') # None if not logged in
325
+
326
+ if user:
327
+ return RedirectResponse('/dashboard')
328
+
329
+ return Title("Welcome"), Container(
330
+ H1("My Awesome App"),
331
+ P("Please login to continue"),
332
+ A("Login", href="/auth/login", cls=ButtonT.primary),
333
+ A("Sign Up", href="/auth/register", cls=ButtonT.secondary)
334
+ )
335
+
336
+ # Protected dashboard
337
+ @app.route("/dashboard")
338
+ def dashboard(req, *args, **kwargs):
339
+ user = req.scope['user']
340
+
341
+ return Title("Dashboard"), Container(
342
+ H1(f"Welcome back, {user.username}!"),
343
+
344
+ # Role-specific content
345
+ Card(
346
+ CardHeader("Your Account"),
347
+ CardBody(
348
+ P(f"Role: {user.role.title()}"),
349
+ P(f"Member since: {user.created_at[:10]}"),
350
+ A("Edit Profile", href="/auth/profile", cls=ButtonT.primary)
351
+ )
352
+ ) if user.role == 'user' else None,
353
+
354
+ # Manager content
355
+ Card(
356
+ CardHeader("Management Tools"),
357
+ CardBody(
358
+ A("View Reports", href="/reports", cls=ButtonT.primary),
359
+ A("Manage Users", href="/users", cls=ButtonT.secondary)
360
+ )
361
+ ) if user.role in ['manager', 'admin'] else None
362
+ )
363
+
364
+ if __name__ == "__main__":
365
+ serve(port=5000)
366
+ ```
367
+
368
+ ## Dependencies
369
+
370
+ - `fasthtml` - Web framework
371
+ - `monsterui` - UI components
372
+ - `fastlite` - Database ORM
373
+ - `bcrypt` - Password hashing
374
+
375
+ ## Development
376
+
377
+ ```bash
378
+ # Clone the repository
379
+ git clone https://github.com/fromlittleacorns/fasthtml-auth.git
380
+ cd fasthtml-auth
381
+
382
+ # Install development dependencies
383
+ pip install -e .[dev]
384
+
385
+ # Run tests
386
+ python -m pytest
387
+
388
+ # Run example
389
+ python examples/complete_app.py
390
+ ```
391
+
392
+ ## Contributing
393
+
394
+ We welcome contributions! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
395
+
396
+ ## License
397
+
398
+ MIT License - see [LICENSE](LICENSE) file for details.
399
+
400
+ ## Changelog
401
+
402
+ ### v0.1.0
403
+ - Initial release
404
+ - Basic authentication system
405
+ - MonsterUI integration
406
+ - Role-based access control
407
+ - User registration and profiles
408
+
409
+ ---
410
+
411
+ **FastHTML-Auth** - Authentication made simple for FastHTML applications.
412
+
413
+ For more examples and documentation, visit: [https://github.com/fromlittleacorns/fasthtml-auth](https://github.com/fromlittleacorns/fasthtml-auth)
@@ -0,0 +1,15 @@
1
+ fasthtml_auth/__init__.py,sha256=6y-RXppiFdNaMAMbWNUxaEZmapDe_gGQfgRW69wINH8,780
2
+ fasthtml_auth/database.py,sha256=xVSqZD8-zA1qq166LsQMQvxMkMaJhLfIv1QIXX0Bgvc,1079
3
+ fasthtml_auth/forms.py,sha256=zlgaq5cmHQspjwyz5e48yuC03JL5D4dSm_6gOKnBnP0,15163
4
+ fasthtml_auth/init.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ fasthtml_auth/manager.py,sha256=1sI6S4HLji9FdZNxPCp-73wsihkyaNvCG_DzibfazDk,2663
6
+ fasthtml_auth/middleware.py,sha256=CplYcPjzkTRcVGXr19ClDG3inoaxHGK5ZmQUWjboCOc,3341
7
+ fasthtml_auth/models.py,sha256=WMZuvW9ikB9y8Zql4Q68ItvzVYxNkGuLWsR5gnPp86w,2323
8
+ fasthtml_auth/repository.py,sha256=27j7ps4RurezPKUjGBMhUGQBofRER2GeSFgSe7FPjic,4395
9
+ fasthtml_auth/routes.py,sha256=6NBLxppYi53SfUjyMnXLb3gzl9GlswqdTZgEg36-FGc,9025
10
+ fasthtml_auth/utils.py,sha256=NsdmkcxVLfahe_BVc8GqawadwJgilX6NbZt-gMdiibk,1451
11
+ fasthtml_auth-0.1.0.dist-info/licenses/LICENSE,sha256=EnPW65jfIuV5msUjRTyzltVV2Aqo3HcIyM6z3hUloXQ,1064
12
+ fasthtml_auth-0.1.0.dist-info/METADATA,sha256=aeBCysMLiSnnlFz3myExT9FH2vMuuW1uFJ7ANMnvKiU,12395
13
+ fasthtml_auth-0.1.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
14
+ fasthtml_auth-0.1.0.dist-info/top_level.txt,sha256=uG6b_v5AONQHyoez8FVziIAf9ORW0Ri_mUaU0yNwMWk,14
15
+ fasthtml_auth-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.9.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 John Richmond
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ fasthtml_auth