Generate Sample Applications

In this guide, learn how to integrate Affinidi Login in different frameworks with the available Sample Applications to get you started quickly.

In this guide, we will use Affinidi CLI to generate a sample application and configure the application to integrate Affinidi Login. The Affinidi Login is a passwordless authentication solution that verifies user identity using Affinidi Vault as the identity provider, owned and managed by the end-user on their edge devices.

The Sample Applications are open source and accessible in this Git repository .

We will use Django framework from the available Sample Applications to take you through the step-by-step guide to generating the app, creating a Login Configuration, and setting up the application to implement a passwordless authentication for end-users.

Before you begin
  1. Set up Affinidi Vault account. Follow the guide below if you haven’t set it up yet.
Set up Affinidi Vault
  1. Set up an Affinidi Vault account using the web app or install the mobile app .

The same installation steps for mobile app.

  1. Click on Start if you are creating a new account, or click on Restore from Backup if you have an existing backup of your Affinidi Vault.

Use this guide to learn how to migrate your existing Affinidi Vault account.

Affinidi Vault Setup
  1. Secure your Vault by providing a passphrase. Use this passphrase to unlock your Vault.
Affinidi Vault Passphrase
  1. Provide your Email Address to verify with OTP.
Affinidi Vault Email Verification

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

  1. Install the Affinidi CLI. Follow the guide below if it hasn’t been installed.
Set up Affinidi CLI
  1. Download and install NodeJS on your machine if you haven’t set it up yet.
  1. Install Affinidi CLI using Node Package Manager (npm).
npm install -g @affinidi/cli
  1. Verify that the installation is successful.
affinidi --version
  1. Make sure you have Git installed on your machine. Follow this guide on how to install Git.

  2. Install Python on your machine if you haven’t installed yet using this guide .

Create Login Configuration

To create a Login Configuration, you can either use Affinidi CLI or  Affinidi Portal.

We use the Redirect URIs configured for the Django sample application that we will generate later. You may refer to this list of settings associated with each sample application available.

Django sample app settings:

Name: Django App

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

Expand the section below for your preferred method:

Using Affinidi CLI
  1. Log in to Affinidi CLI by running:
affinidi start
  1. Once you have successfully logged in, create the Login Configuration by running:
affinidi login create-config --name='Django App' --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.

Using Affinidi Portal
Create new Login Configuratioin
  1. Go to  Affinidi Login under the Services section.

  2. 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.
  1. Click on create and confirm if all the details are correct.
Login Configuratation new client
  1. 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.

  2. 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.

Generate and Set up Sample Application

After creating the Login Configuration required for the sample application, we generate the sample application using Affinidi CLI. Follow the steps below to run the generate app command:

  1. If you haven’t logged in yet to the Affinidi CLI or your session timed out, log in using the following command:
affinidi start

It will generate the necessary access token and session to call Affinidi services.

  1. If you have multiple projects available on your account, select the project where you created the Login Configuration previously. To do this, run the following commands:
affinidi project list-projects

After listing your available projects, get the Project ID of the project you wish to switch and run the following command:

affinidi project select-project --project-id <PROJECT_ID>

Generate Sample App

  1. Generate the sample application using the Django framework and Affinidi as the provider.
affinidi generate app --path "./my-app" --framework django --provider affinidi

After running the command, it will prompt you if you would like to configure the env for the reference app, enter Y.

Automatically configure sample app environment? (Y/n) Y

It will fetch all available Login Configurations on your current project. Select the config you have created previously.

Select a login configuration to use in your reference application

Since the Client Secret is no longer viewable, you must manually set the value generated previously.

What is the login configuration's client secret? ***********************

After that, the sample application is generated with the preconfigured env variable to integrate Affinidi Login.

Set up Sample Application

  1. Install the Application libraries to run the application:
pip install -r requirements.txt

It will install all the required libraries for the sample application.

  1. Run the migration script to propagate changes to the model:
python manage.py migrate

Run Sample Application

  1. Run the application
python manage.py runserver

After successfully running the command, go to http://localhost:8000 to access the page with the Affinidi Login button.

Generate Other Sample Apps

To view other available Sample Applications, go to our Github repository and open the apps.json file. This file also includes the settings required to set up the Login Configuration and the selected Provider.

To generate other Sample Applications, get the framework and provider from the list:

Format: <PROVIDER>-<FRAMEWORK>-*

Sample: affinidi-flask-authlib

Command:

affinidi generate app --path "./my-app" --framework flask --provider affinidi

More about the generate app command in this documentation.

Follow the steps in this guide and refer to the README.md file in the sample app for more details about the specific setup required.

Always refer to the Github repository and check on the available Sample Applications as we develop more samples to help you with your journey of integrating Affinidi Login and enabling passwordless authentication for enhanced security and privacy for your end-users.