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

16
main.py
View File

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