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 @nulix/adonis-2fa

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

node ace configure @nulix/adonis-2fa
  1. Registers the following service provider inside the adonisrc.ts file.

    {
    providers: [
    // ...other providers
    () => import('@nulix/adonis-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 '@nulix/adonis-2fa'
const twoFactorAuthConfig = defineConfig({
issuer: env.get('APP_ISSUER', 'adonis'),
})
export default twoFactorAuthConfig

issuer

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