Model

Model

If you want to configure your User model to follow the basic flow that we recommend, this is the code:

// ...other imports
import encryption from '@adonisjs/core/services/encryption'
import { TwoFactorSecret } from '@nulix/adonis-2fa/types'
export default class User extends compose(BaseModel, AuthFinder) {
// ...other user columns
@column({ consume: (value) => Boolean(value) })
isTwoFactorEnabled: boolean = false
@column({
serializeAs: null,
consume: (value: string) => (value ? encryption.decrypt(value) : null),
prepare: (value: string) => encryption.encrypt(value),
})
declare twoFactorSecret: TwoFactorSecret | null
@column({
serializeAs: null,
consume: (value: string) => (value ? encryption.decrypt(value) : []),
prepare: (value: string[]) => encryption.encrypt(value),
})
declare twoFactorRecoveryCodes: string[]
}
  • The isTwoFactorEnabled will be used to check if the user has enabled the 2FA or not. It is worth mentioning, that you SHOULD NOT set that to true until the user has verified the OTP at least one time, or they will be locked without being able to login in anymore.

  • The twoFactorSecret is the user Secret generated by the lib. Should be encrypted as good practice.

  • The twoFactorRecoveryCodes is the user Recovery Codes generated by the lib. Should be encrypted as good practice.