Files
big-brother/api/commands/user_reset_2fa.ts
T
2026-10-12 14:25:22 +02:00

41 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { BaseCommand, args } from '@adonisjs/core/ace'
import type { CommandOptions } from '@adonisjs/core/types/ace'
/**
* New double authentication secret (lost phone, new app…).
*/
export default class UserReset2fa extends BaseCommand {
static commandName = 'user:reset-2fa'
static description = 'Réinitialiser la double authentification d’un compte'
static options: CommandOptions = {
startApp: true,
}
@args.string({ description: 'Identifiant du compte' })
declare username: string
async run() {
const { default: User } = await import('#models/user')
const { enrollTotp } = await import('#services/totp_enrollment')
const user = await User.findBy('username', this.username)
if (!user) {
this.logger.error(`Compte « ${this.username} » introuvable.`)
this.exitCode = 1
return
}
const totpSecret = await enrollTotp(this, user.username)
if (!totpSecret) {
this.exitCode = 1
return
}
user.totpSecret = totpSecret
user.totpLastStep = null
await user.save()
this.logger.success('Double authentification réinitialisée.')
}
}