Introduction

Introduction

Adonis-2FA is a library for managing Two Factor Authentication in your AdonisJS project build on top of node-2fa.

The package it self does not store any secret or data on your behalf. It only give you access the methods to implement a two factor authentication flow and create recovery codes. You can store that information inside a database and use the auth package to login the user within your application.

But you can generate some sample codes like migrations and API controllers/routes to give you some ideas on how to implement your 2FA flow.

Installation

Install the package from the npm packages registry using one of the following commands.

npm i @mdsadique-inam/adonisjs-2fa

Once the package is installed, you must configure it using the node ace configure command.

node ace configure @mdsadique-inam/adonisjs-2fa
  1. Registers the following service provider inside the adonisrc.ts file.

    {
    providers: [
    // ...other providers
    () => import('@mdsadique-inam/adonisjs-2fa/two_factor_auth_provider'),
    ]
    }
  2. If you choose to create migration, creates database migration for your authentication table (ex: users).

  3. If you choose to create basic API 2FA flow, creates controller, routes and validation to a basic 2FA flow.

  4. Create the config/2fa.ts file.

Configuration

The configuration for 2FA is stored inside the config/2fa.ts file.

See also: Adonis-2FA config stubs

import env from '#start/env'
import {defineConfig} from '@mdsadique-inam/adonisjs-2fa'
const twoFactorAuthConfig = defineConfig({
issuer: env.get('APP_ISSUER', 'adonis'),
/**
* Number of secret bytes that will generate on twoFactorAuth.generateSecret(user.email)
* default value is 20
*/
numberOfSecretBytes: 20,
/**
* Size of the recovery code that will be generated
* default value is 10
*/
recoveryCodeSize: 10,
/**
* The length of recovery codes array
* default is 10
*/
recoveryCodesLength: 10
})
export default twoFactorAuthConfig

issuer

The name of your application that will show in the user 2FA Authenticator.

numberOfSecretBytes
Number of secret bytes that will generate on twoFactorAuth.generateSecret(user.email). If not set it will use default value which is 20
recoveryCodeSize
Size of the recovery code that you want to generate. If not set it will use default value which is 10
recoveryCodesLength
The length of recovery codes array that you want to generate. If not set it will use default value which is 10