HTML & Django Quickstart

This is a quickstart guide to help you integrate Descope with your HTML & Django application. Follow the steps below to get started.

Import SDK

You must import our WebJS and Web Component SDKs in order to use the Descope Flows component.

index.html
<!DOCTYPE html>
<html>
  <head>
	<script src="https://unpkg.com/@descope/web-component@x.x.x/dist/index.js"></script>
	<script src="https://unpkg.com/@descope/web-js-sdk@x.x.x/dist/index.umd.js"></script>
  </head>
 
  <body>
    <h1>Log In With Descope Flows</h1>
    <p id="container"></p>
 
	<script></script>
  </body>
</html>

Import Descope Functions and Add Flows Component

To trigger the Descope Flow, you will need to add this component. The screens you've customized in your Flow will appear here. You can also customize the component with the following:

  • flowId: ID of the flow you wish to use
  • onSuccess and onError: functions that execute when authentication succeeds or fails

Note

If you are using the autoRefresh token feature in the WebJS SDK, you will need to call sdk.refresh() in your onSuccess() function.

You can use the logic below to validate whether or not a user is logged in, before deciding to display the login flow. You can use the following parameters when initializing the SDK:

  • persistTokens: this will force existing tokens to be included in all outgoing requests
  • autoRefresh: this will automatically refresh sessions once they are expired, if the refresh token is valid

Note

For the full list of component customization options, refer to our Descope Components Customization Doc.

index.html
<!DOCTYPE html>
<html>
  <head>
	<script src="https://unpkg.com/@descope/web-component@x.x.x/dist/index.js"></script>
	<script src="https://unpkg.com/@descope/web-js-sdk@x.x.x/dist/index.umd.js"></script>
  </head>
 
  <body>
	<h1>Log In With Descope Flows</h1>
    <p id="container"></p>
 
	<script>
		const sdk = Descope({ projectId: '__ProjectID__', persistTokens: true, autoRefresh: true });
 
		const sessionToken = sdk.getSessionToken()
		var notValidToken
		if (sessionToken) {
			notValidToken = sdk.isJwtExpired(sessionToken)
		}
		if (!sessionToken || notValidToken) {
			var container = document.getElementById('container');
			container.innerHTML = '<descope-wc project-id="__ProjectID__" flow-id="sign-up-or-in"></descope-wc>';
			const onSuccess = (e) => {
				sdk.refresh(),
			};
 
		}
	</script>
  </body>
</html>

Make Sure Session Persists Across All Pages

On all of your frontend pages, you'll need to make sure that sdk.refresh() is called at the top of each page in <script> tags. Add the following logic to every authenticated page you have in your application.

index.html
<!DOCTYPE html>
<html>
  <head><!-- ... --></head>
 
  <body><!-- ... --></body>
</html>
 
<!-- Add below lines to all your authenticated pages -->
<script src="https://unpkg.com/@descope/web-js-sdk@x.x.x/dist/index.umd.js"></script>
<script>
  sdk.refresh()
  // Rest of your logic ...
</script>

Install Backend SDK

Install the SDK with the following command:

Terminal
pip install django-descope

Import and Setup Backend SDK

You'll need install and setup all of the packages from the SDK. This is done by ensuring django_descope is under INSTALLED_APPS in settings.py.

If you're using a CNAME with your Descope project, make sure to export the Base URL (e.g. export DESCOPE_BASE_URI="https://api.descope.com") when initializing descope_client.

settings.py
INSTALLED_APPS = [
  ...
  'django_descope',
]
DESCOPE_PROJECT_ID=os.getenv("DESCOPE_PROJECT_ID")

Add Descope Middleware

Ensure Descope Middleware is after the AuthenticationMiddleware and SessionMiddleware.

settings.py
MIDDLEWARE = [
  ...
  'django.contrib.sessions.middleware.SessionMiddleware',
  'django.contrib.auth.middleware.AuthenticationMiddleware',
  ...
  'django_descope.middleware.DescopeMiddleware',
]

Configure URLconf

You will then need to include the Descope URLconf in your project urls.py like this.

urls.py
path('auth/', include('django_descope.urls')),

Configure URLconf

The session validation is handled in the Django SDK, through the middleware.

settings.py
INSTALLED_APPS = [
  ...
  'django_descope',
]
MIDDLEWARE = [
  ...
  'django.contrib.sessions.middleware.SessionMiddleware',
  'django.contrib.auth.middleware.AuthenticationMiddleware',
  ...
  'django_descope.middleware.DescopeMiddleware',
]
DESCOPE_PROJECT_ID=os.getenv("DESCOPE_PROJECT_ID")

Congratulations

Now that you've got the authentication down, go focus on building out the rest of your app!

Here is a sample application using the Django SDK.


Checkpoint

Your application is now integrated with Descope. Please test with sign-up or sign-in use case.

Need help?

Customize

Now that you have the end-to-end application working, you can choose to configure and personalize many different areas of Descope, including your brand, style, custom user authentication journeys, etc. We recommend starting with customizing your user-facing screens, such as signup and login.

Was this helpful?

On this page