# Affinidi Login Integration using Django and Authlib

> Use this guide to learn how to integrate Affinidi Login on your projects.

[ Affinidi](https://www.affinidi.com/product/affinidi-login)
                [ Python](https://www.python.org/)
                [ Django](https://www.djangoproject.com/)
                [ authlib](https://authlib.org/)

A code sample using Django framework and uses AuthLib library to enable passwordless login using Affinidi IDP.

## Before you begin

- Set up Affinidi Vault account. Follow the guide below if you haven’t set it up yet.

        Set up Affinidi Vault

Set up an Affinidi Vault account using the [Web Vault](https://vault.affinidi.com) or install the Mobile Vault (for [Android](https://play.google.com/store/apps/details?id=com.affinidi.vault&pcampaignid=web_share)).

The same setup steps for Mobile Vault.

- 
Click on Get started if you are creating a new account, or click on Restore from Backup if you have an existing backup of your Affinidi Vault. Provide the passphrase to secure your Affinidi Vault.

You have the option to enable Biometrics to unlock your Affinidi Vault easily instead of using passphrase.

GIF 

- Enter your email address to register with the Affinidi Vault. An OTP will be sent to this email for verification.

GIF 

- Enter the OTP sent to the email you have provided for verification to complete the setup.

GIF 

After successfully providing the OTP, you are redirected to the Affinidi Vault dashboard.
Important Note

Remember to keep your passphrase in a secure location. Use the Passphrase Reset feature in Affinidi Vault settings to generate the PDF files and keep them safe, which you can use to recover access to your Affinidi Vault if you forget your passphrase.

- Install the Affinidi CLI. Follow the guide below if it hasn’t been installed.

        Set up Affinidi CLI

- Download and install [NodeJS](https://nodejs.org/en/download) on your machine if you haven’t set it up yet.

Node Version

Affinidi CLI requires Node version 18 and above.

- Install Affinidi CLI using Node Package Manager (npm).

```bash
npm install -g @affinidi/cli
```

- Verify that the installation is successful.

```bash
affinidi --version
```

- Make sure you have Git installed on your machine. Follow [this guide](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) on how to install Git.

## Download the Application

You can download as ZIP file the code sample from the [GitHub Repo](https://github.com/affinidi/reference-app-affinidi-vault/tree/main/samples/affinidi-django-authlib) or generate it using Affinidi CLI with the following command:

```Bash
affinidi generate app --provider=affinidi --framework=django --library=authlib --path=affinidi-login-refcodes
```

Select n when prompted to Automatically configure sample app environment, we will configure it later.

The above command will generate the code sample in the affinidi-login-refcodes directory.

    Important Note
    The downloadable sample application is provided only as a guide to quickly explore and learn how to integrate the components of Affinidi Trust Network into your application. This is NOT a Production-ready implementation. Do not deploy this to a production environment.

## Install Dependencies

After successfully generating the code sample, go to the affinidi-login-refcodes directory and install the required dependencies using the following commands:

```Bash
pip install -r requirements.txt
```

## Create Login Configuration

Name: Affinidi Login Sample

Redirect URIs: http://localhost:8000/callback

        Using Affinidi CLI

- Log in to Affinidi CLI by running:

```Bash
affinidi start
```

- Once you have successfully logged in, create the Login Configuration by running:

```Bash
affinidi login create-config --name='Affinidi Login Sample' --redirect-uris='http://localhost:8000/callback'
```

- --name is what you want your login configuration to be called.

- --redirect-uris is the URL on your application where the user gets redirected after the successful authentication.

Learn more on how to manage your Login Configurations using [Affinidi CLI](/dev-tools/affinidi-cli/manage-login.md).

        Using Affinidi Portal

- 
Go to [Affinidi Login ](https://portal.affinidi.com/affinidiLogin) under the Services section.

- 
Click on the Create Login Configuration and provide the required details.

- Name is the string that describes your login configuration.

- Redirect URIs is the URL on your application where the user gets redirected after the successful authentication.

- 
Click on create and confirm if all the details are correct.

- 
After confirming the details, another popup shows the Client ID and Client Secret for your Login Configuration. Copy the generated Client Credentials and use them to integrate with Affinidi Login.

- After copying the Client ID and Client Secret and closing the popup, you are redirected back to the Affinidi Login page.

Login Configuration uses the default Presentation Definition (presentationDefinition) and ID Token Mapping (idTokenMapping) that is used to request the user’s email address during the authentication flow.

Learn more about customising the Presentation Definition and ID Token using [this guide](/products/affinidi-elements/affinidi-login/presentation-definition-id-token-mapping.md).

    Important

Safeguard the Client ID and Client Secret diligently; you'll need them for setting up your IdP or OIDC-compliant applications. Remember, the Client Secret will be provided only once.

## Set up the Application

Once the Login Configuration is created, set up the client credentials provided to integrate Affinidi Login.

Copy and set up the environment variables:

```Bash
cp .env.example .env
```

Set the following variables with the values provided by the Login Configuration:

```Bash
PROVIDER_CLIENT_ID=""
PROVIDER_CLIENT_SECRET=""
PROVIDER_ISSUER=""
```

The <LoginConfig.auth.*> are values from Login Configuration.

Then run the migrate command:

```Bash
python manage.py migrate
```

## Run the Application

After installing the dependencies and setting up the required details in the application, run the following command to start the app locally:

```Bash
python manage.py runserver
```

Once it is successfully started, visit the app using the link [http://localhost:8000](http://localhost:8000).

## Integration-related Changes

To enable a seamless passwordless login experience with Affinidi Login, refer to the following key changes were implemented:

- Imported Authlib to enable OAuth flow.

- Implemented the following function in the ./django_reference_app/views.py:

- Registered affinidi as OAuth provider with the client credentials.

- login function to initiate the Affinidi Login flow using Authlib.

- callback function to receive the response from Affinidi Login (e.g. idToken, accessToken).

- Parsed the environment variables in the ./django_reference_app/settings.py.

Explore the sample implementation to learn more about how the integration works.
