Flask Tutorial: Simple User Registration and Login

Joël Franusic

Hi Raghav. Sorry about the issues you were running into. I was able to get the code in this blog post working after making two changes:

1: Removing the period (".") before the names the url_for() functions
2: Changing app.config[“SECRET_KEY”] to app.secret_key

Please make sure that your app.py looks like the one below:


from flask import Flask, render_template, g, redirect, url_for
from flask_oidc import OpenIDConnect
from okta import UsersClient

app = Flask(name)
app.config[“OIDC_CLIENT_SECRETS”] = “client_secrets.json"
app.config[“OIDC_COOKIE_SECURE”] = False
app.config[“OIDC_CALLBACK_ROUTE”] = “/oidc/callback"
app.config[“OIDC_SCOPES”] = [“openid”, “email”, “profile”]
app.secret_key = “0averylongrandomstring"
app.config[“OIDC_ID_TOKEN_COOKIE_NAME”] = “oidc_token"
oidc = OpenIDConnect(app)
okta_client = UsersClient(“https://dev-1234567.okta.co”, “00yourtokenhere”)

@app.before_request
def before_request():
if oidc.user_loggedin:
g.user = okta_client.get_user(oidc.user_getfield(“sub”))
else:
g.user = None

@app.route(”/”)
def index():
return render_template(“index.html”)

@app.route(”/dashboard”)
@oidc.require_login
def dashboard():
return render_template(“dashboard.html”)

@app.route("/login")
@oidc.require_login
def login():
return redirect(url_for(“dashboard”))

@app.route("/logout")
def logout():
oidc.logout()
return redirect(url_for(“index”))

Here is what the diff command shows as being different between these two files


— a/app.py
+++ b/app.py
@@ -7,10 +7,10 @@ app.config[“OIDC_CLIENT_SECRETS”] = “client_secrets.json"
app.config[“OIDC_COOKIE_SECURE”] = False
app.config[“OIDC_CALLBACK_ROUTE”] = “/oidc/callback"
app.config[“OIDC_SCOPES”] = [“openid”, “email”, “profile”]
-app.config[“SECRET_KEY”] = “{{ LONG_RANDOM_STRING }}”
+app.secret_key = “0averylongrandomstring"
app.config[“OIDC_ID_TOKEN_COOKIE_NAME”] = “oidc_token"
oidc = OpenIDConnect(app)
-okta_client = UsersClient(”{{ OKTA_ORG_URL }}”, “{{ OKTA_AUTH_TOKEN }}”)
+okta_client = UsersClient(“https://dev-1234567.okta.com”, “00yourtokenhere”)
@app.before_request
@@ -35,10 +35,10 @@ def dashboard():
@app.route(”/login”)
@oidc.require_login
def login():
- return redirect(url_for(".dashboard"))
+ return redirect(url_for(“dashboard”))
@app.route("/logout")
def logout():
oidc.logout()
- return redirect(url_for(".index"))
+ return redirect(url_for(“index”))