Skip to content

Commit

Permalink
Completed aiohttp app
Browse files Browse the repository at this point in the history
  • Loading branch information
shane-mason committed Oct 13, 2017
1 parent f31bb4e commit ee63350
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
23 changes: 17 additions & 6 deletions examples/aiohttp_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ async def hello(request):
return web.Response(text="Hello, world")



def logout(request):
request.app['auth'].end_session(request['token'])
response = aiohttp_jinja2.render_template('login.html', request, {'logged_in': False})
response.del_cookie('token')
return response

async def login(request):
context = {}
Expand All @@ -47,13 +51,19 @@ async def login(request):
data = await request.post()
login_name = data['login_name']
passphrase = data['passphrase']
token = request.app['auth'].start_session(login_name, passphrase)

if token:
context = {'logged_in': True}
else:
context = {'logged_in' : False}
try:

token = request.app['auth'].start_session(login_name, passphrase)

if token:
context = {'logged_in': True}
else:
context = {'logged_in' : False}

except VerificationFailedException:
context = {'logged_in' : False, 'verify_failed': True}
token = None
else:
valid = False
if 'token' in request.cookies:
Expand All @@ -75,6 +85,7 @@ async def login(request):
app.router.add_get('/', hello)
app.router.add_post('/login', login)
app.router.add_get('/login', login)
app.router.add_get('/logout', logout)

app.on_startup.append(startup)
web.run_app(app, port=8080)
Expand Down
9 changes: 7 additions & 2 deletions examples/templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@

{% if not logged_in %}

<h4>You Are Not Logged In</h4>
{% if not verify_failed %}
<h4>You Have Not Logged In</h4>
{% else %}
<h4>Your Login Failed</h4>
{% endif %}
<form action="/login" method="post" accept-charset="utf-8" enctype="application/x-www-form-urlencoded">

<div class="container">
Expand All @@ -88,7 +92,8 @@ <h4>You Are Not Logged In</h4>
</div>
</form>
{% else %}
<h4>Already Logged In!</h4>
<h4>Already Logged In!</h4>
<a href="/logout">Logout</a>
{% endif %}
</body>
</html>

0 comments on commit ee63350

Please sign in to comment.