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
isTwoFactorEnabledwill be used to check if the user has enabled the 2FA or not. It is worth mentioning, that youSHOULD NOTset that totrueuntil the user has verified the OTP at least one time, or they will be locked without being able to login in anymore. -
The
twoFactorSecretis the userSecretgenerated by the lib. Should be encrypted as good practice. -
The
twoFactorRecoveryCodesis the userRecovery Codesgenerated by the lib. Should be encrypted as good practice.