Implement login

This commit is contained in:
Kai Vogelgesang 2020-04-17 03:10:52 +02:00
parent f966ee4abc
commit 4886f78357
2 changed files with 13 additions and 23 deletions

View File

@ -10,22 +10,16 @@
<body>
this is the base site
<a href="/newclient">Log in</a>
<br>
<input id="some_input" type="text" placeholder="dunno"/>
<button id="btn_dunno">idk</button>
<input id="username" type="text" placeholder="Joe"/>
<button id="btn_register">idk</button>
<script>
document.getElementById("btn_dunno").onclick = async function (e) {
let text = document.getElementById("some_input").value;
let data = {"username": text};
document.getElementById("btn_register").onclick = function (e) {
let username = document.getElementById("username").value;
await fetch('/newclient', {
method: 'POST',
body: JSON.stringify(data),
})
window.location.replace(`/register/${encodeURIComponent(username)}`)
};
</script>
</body>

20
main.py
View File

@ -1,5 +1,6 @@
import aiohttp.web
import traceback
import urllib.parse
from model import Model
@ -31,7 +32,7 @@ def get_client(request: aiohttp.web.Request):
async def handler(request: aiohttp.web.Request):
# this handler prevents people missing trailing slashes
client = get_client(request)
raise aiohttp.web.HTTPFound(f"/{client}/")
raise aiohttp.web.HTTPFound(f"{client}/")
@routes.get(CLIENT_REGEX + '/')
@ -95,19 +96,14 @@ async def handler(request):
return aiohttp.web.FileResponse('index.html')
@routes.post('/newclient')
@routes.get('/register/{username}')
async def handler(request):
model = request.app['model']
data = await request.json()
username = data.get("username")
print(f"{username=}")
if username:
client = model.create_client(username)
print(f"werked?")
raise aiohttp.web.HTTPFound(f"/{client}/")
print(f"werked?")
else:
raise aiohttp.web.HTTPFound(f"/newclient")
username = request.match_info.get('username', 'Joe')
username = urllib.parse.unquote(username)
client = model.create_client(username)
raise aiohttp.web.HTTPFound(f"/{client}/")
if __name__ == '__main__':