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
register.py · 896 bytes · 25 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 | name = request.param('name', '') |
| 13 | |
| 14 | try: |
| 15 | user = accounts.register(email, password, name=name) |
| 16 | token = create_session(str(user.uid)) |
| 17 | |
| 18 | # Set session cookie |
| 19 | status, headers, body = redirect('/') |
| 20 | headers.append(('set-cookie', f'session={token}; HttpOnly; Max-Age=2592000; Path=/')) |
| 21 | return status, headers, body |
| 22 | except ValueError as e: |
| 23 | return html(render('register.html', error=str(e))) |
| 24 | |
| 25 | return html(render('register.html')) |