| title | Auth0 Python SDK Web App Tutorial | ||||||
|---|---|---|---|---|---|---|---|
| description | This tutorial will show you how to use the Auth0 Python SDK to add authentication and authorization to your web app. | ||||||
| name | Python | ||||||
| image | /media/platforms/python.png | ||||||
| tags |
|
||||||
| snippets |
|
||||||
| seo_alias | python |
You can get started by either downloading the seed project or if you would like to add Auth0 to an existing application you can follow the tutorial steps.
::: panel-info System Requirements This tutorial and seed project have been tested with the following:
- Python 2.7
- Flask 0.10.1
- Requests 2.3.0 :::
<%= include('../_includes/_package', { pkgRepo: 'auth0-python', pkgBranch: 'master', pkgPath: 'examples/flask-webapp', pkgFilePath: null, pkgType: 'server' }) %>
Otherwise, please follow the steps below to configure your existing Python WebApp to use it with Auth0.
${snippet(meta.snippets.dependencies)}
This example uses flask but it could work with any server
You'll need to create a callback handler that Auth0 will call once it redirects to your app. For that, you can do the following:
${snippet(meta.snippets.setup)}
${include('./_callbackRegularWebApp')}
In this case, the callbackURL should look something like:
http://yourUrl/callback
${lockSDK}
Note: Please note that the
callbackURLspecified in theAuth0Lockconstructor must match the one specified in the previous step
You can access the user information via the profile you stored in the session on step 2
@app.route("/dashboard")
@requires_auth
def dashboard():
return render_template('dashboard.html', user=session['profile'])<div>
<img class="avatar" src="{{user['picture']}}"/>
<h2>Welcome {{user['nickname']}}</h2>
</div>Click here to check all the information that the userinfo hash has.
You have configured your Python Webapp to use Auth0. Congrats, you're awesome!
You can add the following annotation to your Flask app to check if the user is authenticated. Note that you should import wraps first, adding the following line to your file from functools import wraps.
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
if 'profile' not in session:
# Redirect to Login page here
return redirect('/')
return f(*args, **kwargs)
return decoratedWe've actually used the annotation on step 5.