30 lines
1.1 KiB
Python
30 lines
1.1 KiB
Python
|
|
from typing import Any, Dict, Union
|
|
|
|
from matemat.exceptions import AuthenticationError, HttpException
|
|
from matemat.webserver import pagelet, RequestArguments, PageletResponse, RedirectResponse, TemplateResponse
|
|
from matemat.primitives import User
|
|
from matemat.db import MatematDatabase
|
|
|
|
|
|
@pagelet('/login')
|
|
def login_page(method: str,
|
|
path: str,
|
|
args: RequestArguments,
|
|
session_vars: Dict[str, Any],
|
|
headers: Dict[str, str],
|
|
config: Dict[str, str])\
|
|
-> Union[bytes, str, PageletResponse]:
|
|
if 'user' in session_vars:
|
|
return RedirectResponse('/')
|
|
if method == 'GET':
|
|
return TemplateResponse('login.html', setupname=config['InstanceName'])
|
|
elif method == 'POST':
|
|
with MatematDatabase(config['DatabaseFile']) as db:
|
|
try:
|
|
user: User = db.login(str(args.username), str(args.password))
|
|
except AuthenticationError:
|
|
return RedirectResponse('/login')
|
|
session_vars['user'] = user
|
|
return RedirectResponse('/')
|
|
raise HttpException(405)
|