add logout button

This commit is contained in:
iou1name 2020-11-18 13:09:09 -05:00
parent 60c65c7044
commit 976ffe4551
3 changed files with 22 additions and 0 deletions

View File

@ -347,6 +347,22 @@ async def set_session(request):
return web.json_response(error)
@routes.get('/logout', name='logout')
@auth.auth_required
async def logout(request):
"""Deletes the users session cookie."""
async with request.app['pool'].acquire() as conn:
await conn.execute(
"DELETE FROM user_session "
"WHERE id = $1 AND user_id = $2",
request.cookies.get('session'), request['session']['id'])
login_url = request.app.router['login'].url_for()
resp = web.HTTPFound(location=login_url)
resp.set_cookie('userid', '', domain=config.server_domain, max_age=0)
resp.set_cookie('session', '', domain=config.server_domain, max_age=0)
raise resp
async def init_app():
"""Initializes the application."""
app = web.Application()

View File

@ -17,6 +17,11 @@ header {
margin-right: 1em;
}
#username {
margin-left: auto;
margin-top: auto;
}
main {
display: grid;
gap: 2em;

View File

@ -15,6 +15,7 @@
<header>
<object id="logo" title="Buckler logo" data="/static/buckler_icon.svg"></object>
<h1>Buckler</h1>
<span id="username">Logged in as: <b>{{ request['session']['username'] }}</b> (<a href="./logout">Logout</a>)</span>
</header>
<main>
<section>