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 youSHOULD NOT
set that totrue
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 userSecret
generated by the lib. Should be encrypted as good practice. -
The
twoFactorRecoveryCodes
is the userRecovery Codes
generated by the lib. Should be encrypted as good practice.