This tutorial shows how to add passkeys to a Python Django web app. We use Corbado's passkey-first UI component and implement simple session management.
Nicolai
Created: November 30, 2023
Updated: August 29, 2024
We aim to make the Internet a safer place using passkeys. That's why we want to support developers with tutorials on how to implement passkeys.
2. Django Passkey Project Prerequisites
3. Repository Structure for Django Passkey Project
4. Set Up Your Corbado Account and Project
5. Django Passkey Project Setup
5.2. Configure Environment Variables
5.3. Create Templates With Session Management
5.4. Create Django Controller and Configure Routes
In this tutorial, well be walking through the process of building a sample application with passkey authentication using the Django web framework made for Python. To make passkeys work, we use Corbado's passkey-first UI component that automatically connects to a passkeys backend.
If you want to run the project straight away, please follow the README of our sample app Github repository.
The result looks as follows:
Recent Articles
⚙️
FastAPI Passkeys: How to Implement Passkeys with FastAPI
⚙️
Flask Passkeys: How to Implement Passkeys with Python Flask
⚙️
Passkey Tutorial: How to Implement Passkeys in Web Apps
⚙️
Keycloak Passkeys: Add Passkeys To Your Existing Keycloak Users
⚙️
Laravel Passkeys: How to Implement Passkeys in PHP Laravel
This tutorial assumes basic familiarity with Django and Python as well as HTML and JavaScript. Let's dive in!
Become part of our Passkeys Community for updates and support.
JoinOur Django project contains many files, but these are the most important ones:
├── .env # Contains all environment variables ├── passkeys_demo | ├── settings.py # Global settings | ├── urls.py # Route config | ├── views.py # Controller for our pages | ├── templates | | ├── index.html # Login page | | └── profile.html # Profile page
Visit the Corbado developer panel to sign up and create your account (you'll see the passkey sign-up in action here!).
After sign-up, a project wizard will guide you through the necessary steps to get everything up and running:
Application URL
and Relying Party ID
. The Application URL
is the URL where you embed the Corbado UI component. In this example, we set it to http://localhost:8000
. The Relying Party ID
is the domain (no protocol, no port and no path) where passkeys should be bound to. Here, it's localhost
(you can define both values als in the Settings > General > URLs of the Corbado developer panel).Afterwards, you'll see the relevant HTML / JavaScript code snippets you need to integrate into the project. The subsequent sections of this article will explain them in detail.
As another step, we create an API secret
which will be needed to request user data from the
Corbado backend. Please create an API secret
in Settings > Credentials > API secrets.
If you haven't installed Django yet, do so by executing
Install the latest version of the Corbado Python SDK (passkeys):
To initialize our project, we create a new Django project with
This will create a passkeys_demo
folder containing our Django project.
We head into our project with
and create a .env
file in the passkeys_demo
subfolder with the following contents (using your own values of
course):
We use environ to load the key-value pairs from the .env
file into our
environment variables. Install it with
Then, we can import it in our passkeys_demo/settings.py
file and use it to
obtain the variables from `.env and add them as actual environment variables in
Django:
Our templates are placed in passkeys_demo/templates
(You must create the
templates folder first). The login page will be in
passkeys_demo/templates/index.html
. It contains a script from Corbado as well
as the Corbado UI component:
Our second page is the profile page where the user will be redirected to after
authentication. Here, we show some basic user info which we will obtain in the
controller beforehand (we will create the controller later). We also provide
a logout button which will terminate the session Corbado had initiated after
authentication. The page is located at passkeys_demo/templates/profile.html
with the following content:
We will now create the controller for the pages we created a step earlier. In
the passkeys_demo
folder create a views.py
file. It needs two methods: one
for the index page and one for the profile page. Inside the index method, we
only need to inject the Project ID
, but inside the profile method, we need to
verify the integrity of the Corbado session and extract the data stored in it.
For this, we use the Corbado Python SDK sessions
service:
The views.py
file should look like this:
As a last step, we configure the routes for the two methods. Inside
passkeys_demo/urls.py
, we set a route for the login page and one for the
profile page as well as a redirect for any other route:
Subscribe to our Passkeys Substack for the latest news, insights and strategies.
SubscribeNow, run your Django application using the following command:
Your Django application will start and when visiting http://localhost:8000 with a web browser, you should see the Corbado UI component:
After successful sign-up / login, you see the profile page:
This tutorial showed how easy it is to add passwordless authentication with passkeys to a Django app with using Corbado. Besides the passkey-first authentication, Corbado provides simple session management, that we used for a retrieval of basic user data. If you want to read more about how Corbado's session management please check the docs here.
Table of Contents
Enjoyed this read?
🤝 Join our Passkeys Community
Share passkeys implementation tips and get support to free the world from passwords.
🚀 Subscribe to Substack
Get the latest news, strategies, and insights about passkeys sent straight to your inbox.
We provide UI components, SDKs and guides to help you add passkeys to your app in <1 hour
Start for free
Recent Articles
Flask Passkeys: How to Implement Passkeys with Python Flask
Janina - September 15, 2023
FastAPI Passkeys: How to Implement Passkeys with FastAPI
Nicolai - December 19, 2023
Keycloak Passkeys: Add Passkeys To Your Existing Keycloak Users
Nicolai - November 23, 2023