Skip to content

Commit 1937fea

Browse files
authored
Merge pull request auth0-samples#17 from Amialc/logout
added logout functionality, fixes auth0-samples#15
2 parents 1c249fc + 919ad30 commit 1937fea

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

00-Starter-Seed/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ To run the sample, make sure you have `python` and `pip` installed.
88

99
Rename `.env.example` to `.env` and populate it with the client ID, domain, secret, and audience for your Auth0 app. If you are not implementing any API you can use `https://YOUR_DOMAIN.auth0.com/userinfo` as the audience. Also, add the callback URL to the settings section of your Auth0 client.
1010

11+
Register `http://localhost:3000/callback` as `Allowed Callback URLs` and `http://localhost:3000` as `Allowed Logout URLs` in your app settings.
12+
1113
Run `pip install -r requirements.txt` to install the dependencies and run `python server.py`. The app will be served at [http://localhost:3000/](http://localhost:3000/).
1214

1315
## What is Auth0?

00-Starter-Seed/public/app.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ $(document).ready(function() {
1212
scope: 'openid profile',
1313
responseType: 'code',
1414
redirectUri: AUTH0_CALLBACK_URL
15-
});
15+
});
1616
});
17+
18+
$('.btn-logout').click(function(e) {
19+
e.preventDefault();
20+
window.location.href = '/logout';
21+
})
1722
});

00-Starter-Seed/server.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Python Flask WebApp Auth0 integration example
22
"""
33
from functools import wraps
4+
from urllib.parse import urlparse
45
from os import environ as env, path
56
import json
67

@@ -46,8 +47,14 @@ def home():
4647
@requires_auth
4748
def dashboard():
4849
return render_template('dashboard.html',
49-
user=session[constants.PROFILE_KEY])
50+
user=session[constants.PROFILE_KEY], env=env)
5051

52+
@APP.route('/logout')
53+
def logout():
54+
session.clear()
55+
parsed_base_url = urlparse(AUTH0_CALLBACK_URL)
56+
base_url = parsed_base_url.scheme + '://' + parsed_base_url.netloc
57+
return redirect('https://%s/v2/logout?returnTo=%s&client_id=%s' % (AUTH0_DOMAIN, base_url, AUTH0_CLIENT_ID))
5158

5259
@APP.route('/public/<path:filename>')
5360
def static_files(filename):

00-Starter-Seed/templates/dashboard.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,16 @@
55
<!-- font awesome from BootstrapCDN -->
66
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
77
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
8-
8+
<script src="http://code.jquery.com/jquery-3.1.0.min.js" type="text/javascript"></script>
9+
<script src="https://cdn.auth0.com/js/auth0/8.6.0/auth0.min.js"></script>
10+
<script src="/public/app.js"> </script>
911
<link href="/public/app.css" rel="stylesheet">
12+
<script>
13+
var AUTH0_CLIENT_ID = '{{env.AUTH0_CLIENT_ID}}';
14+
var AUTH0_DOMAIN = '{{env.AUTH0_DOMAIN}}';
15+
var AUTH0_CALLBACK_URL = '{{env.AUTH0_CALLBACK_URL if env.AUTH0_CALLBACK_URL else "http://localhost:3000/callback" }}';
16+
var API_AUDIENCE = '{{env.API_ID}}';
17+
</script>
1018
</head>
1119
<body class="home">
1220
<div class="container">
@@ -15,6 +23,7 @@
1523
<h1 id="logo"><img src="//cdn.auth0.com/samples/auth0_logo_final_blue_RGB.png" /></h1>
1624
<img class="avatar" src="{{user['picture']}}"/>
1725
<h2>Welcome {{user['nickname']}}</h2>
26+
<a class="btn btn-primary btn-lg btn-logout btn-block">Logout</a>
1827
</div>
1928
</div>
2029
</div>

0 commit comments

Comments
 (0)