diff --git a/buckler.py b/buckler.py index 3339e98..2a20d9b 100644 --- a/buckler.py +++ b/buckler.py @@ -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() diff --git a/static/buckler.css b/static/buckler.css index fa419ec..771ed0e 100644 --- a/static/buckler.css +++ b/static/buckler.css @@ -17,6 +17,11 @@ header { margin-right: 1em; } +#username { + margin-left: auto; + margin-top: auto; +} + main { display: grid; gap: 2em; diff --git a/templates/index.html b/templates/index.html index 88797f0..bbdcad2 100644 --- a/templates/index.html +++ b/templates/index.html @@ -15,6 +15,7 @@

Buckler

+ Logged in as: {{ request['session']['username'] }} (Logout)