askrobots / dbbasic-examples
Public
Example DBBasic apps (an API, a blog and a microblog) and a template for new DBBasic modules.
danq9
Add official module template for creating DBBasic packages
17cf88a
1 year, 1 month ago
History
login.py · 803 bytes · 24 lines
| 1 | from dbbasic_web.responses import html, redirect |
| 2 | from dbbasic_web.templating import render |
| 3 | from dbbasic_sessions import create_session |
| 4 | from dbbasic_accounts import Accounts |
| 5 | |
| 6 | accounts = Accounts('.', domain='localhost') |
| 7 | |
| 8 | def handle(request): |
| 9 | if request.method == 'POST': |
| 10 | email = request.param('email', '') |
| 11 | password = request.param('password', '') |
| 12 | |
| 13 | user = accounts.login(email, password) |
| 14 | if not user: |
| 15 | return html(render('login.html', error='Invalid credentials')) |
| 16 | |
| 17 | token = create_session(str(user.uid)) |
| 18 | |
| 19 | # Set session cookie |
| 20 | status, headers, body = redirect('/') |
| 21 | headers.append(('set-cookie', f'session={token}; HttpOnly; Max-Age=2592000; Path=/')) |
| 22 | return status, headers, body |
| 23 | |
| 24 | return html(render('login.html')) |