Skip to content

Commit bebe3a5

Browse files
committed
Implement feedback from @adamjmcgrath
1 parent 963b8f3 commit bebe3a5

5 files changed

Lines changed: 24 additions & 175 deletions

File tree

01-Login/.env.example

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
AUTH0_CLIENT_ID={CLIENT_ID}
22
AUTH0_DOMAIN={DOMAIN}
3-
AUTH0_CLIENT_SECRET={CLIENT_SECRET}
4-
AUTH0_CALLBACK_URL=http://localhost:3000/callback
53
AUTH0_AUDIENCE=
64
APP_SECRET_KEY=ALongRandomlyGeneratedString

01-Login/public/app.css

Lines changed: 0 additions & 98 deletions
This file was deleted.

01-Login/server.py

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
if ENV_FILE:
1515
load_dotenv(ENV_FILE)
1616

17-
app = Flask(__name__, static_url_path="/public", static_folder="./public")
17+
app = Flask(__name__)
1818
app.secret_key = env.get("APP_SECRET_KEY")
19-
app.debug = True
2019

2120

2221
@app.errorhandler(Exception)
@@ -33,13 +32,9 @@ def fetch_token(name, request):
3332

3433
oauth = OAuth(app)
3534

36-
auth0 = oauth.register(
35+
oauth.register(
3736
"auth0",
3837
client_id=env.get("AUTH0_CLIENT_ID"),
39-
client_secret=env.get("AUTH0_CLIENT_SECRET"),
40-
api_base_url="https://" + env.get("AUTH0_DOMAIN"),
41-
access_token_url="https://" + env.get("AUTH0_DOMAIN") + "/oauth/token",
42-
authorize_url="https://" + env.get("AUTH0_DOMAIN") + "/authorize",
4338
client_kwargs={
4439
"scope": "openid profile email",
4540
},
@@ -53,43 +48,28 @@ def fetch_token(name, request):
5348
# Controllers API
5449
@app.route("/")
5550
def home():
56-
if "profile" in session:
57-
return render_template(
58-
"dashboard.html",
59-
userinfo=session["profile"],
60-
userinfo_pretty=json.dumps(session["jwt_payload"], indent=4),
61-
)
62-
63-
return render_template("home.html")
64-
51+
return render_template("home.html", session=session.get('user'), pretty=json.dumps(session.get('user'), indent=4))
6552

66-
@app.route("/callback")
67-
def callback_handling():
68-
auth0.authorize_access_token()
69-
resp = auth0.get("userinfo")
70-
userinfo = resp.json()
7153

72-
session["jwt_payload"] = userinfo
73-
session["profile"] = {
74-
"user_id": userinfo["sub"],
75-
"name": userinfo["name"],
76-
"picture": userinfo["picture"],
77-
}
54+
@app.route("/callback", methods=["GET","POST"])
55+
def callback():
56+
token = oauth.auth0.authorize_access_token()
57+
session["user"] = token
7858
return redirect("/")
7959

8060

8161
@app.route("/login")
8262
def login():
83-
return auth0.authorize_redirect(
84-
redirect_uri=env.get("AUTH0_CALLBACK_URL"), audience=env.get("AUTH0_AUDIENCE")
63+
return oauth.auth0.authorize_redirect(
64+
redirect_uri=url_for("callback", _external=True), audience=env.get("AUTH0_AUDIENCE")
8565
)
8666

8767

8868
@app.route("/logout")
8969
def logout():
9070
session.clear()
9171
return redirect(
92-
auth0.api_base_url
72+
"https://" + env.get("AUTH0_DOMAIN")
9373
+ "/v2/logout?"
9474
+ urlencode(
9575
{

01-Login/templates/dashboard.html

Lines changed: 0 additions & 23 deletions
This file was deleted.

01-Login/templates/home.html

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
<html>
2-
<head>
3-
4-
<meta name="viewport" content="width=device-width, initial-scale=1">
5-
6-
<!-- font awesome from BootstrapCDN -->
7-
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
8-
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">
9-
10-
<link href="/public/app.css" rel="stylesheet">
11-
</head>
12-
<body class="home">
13-
<div class="container">
14-
<div class="login-page clearfix">
15-
<div class="login-box auth0-box before">
16-
<img src="https://i.cloudup.com/StzWWrY34s.png" />
17-
<h3>Auth0 Example</h3>
18-
<p>Zero friction identity infrastructure, built for developers</p>
19-
<a id="qsLoginBtn" class="btn btn-primary btn-lg btn-login btn-block" href="/login">Log In</a>
20-
</div>
21-
</div>
22-
</div>
23-
</body>
2+
<head>
3+
<meta charset="utf-8" />
4+
<title>Auth0 Example</title>
5+
</head>
6+
<body>
7+
{% if session %}
8+
<h1>Welcome {{session.userinfo.name}}!</h1>
9+
<p><a href="/logout">Logout</a></p>
10+
<div><pre>{{pretty}}</pre></div>
11+
{% else %}
12+
<h1>Welcome Guest</h1>
13+
<p><a href="/login">Login</a></p>
14+
{% endif %}
15+
</body>
2416
</html>

0 commit comments

Comments
 (0)