Files
2026-10-12 14:25:22 +02:00

40 lines
1.2 KiB
TypeScript
Raw Permalink 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'
export default class UserPassword extends BaseCommand {
static commandName = 'user:password'
static description = 'Changer le mot de passe 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 user = await User.findBy('username', this.username)
if (!user) {
this.logger.error(`Compte « ${this.username} » introuvable.`)
this.exitCode = 1
return
}
const password = await this.prompt.secure('Nouveau mot de passe (12 caractères minimum)', {
validate: (value) => value.length >= 12 || 'Au moins 12 caractères',
})
const confirmation = await this.prompt.secure('Confirme le mot de passe')
if (password !== confirmation) {
this.logger.error('Les mots de passe ne correspondent pas.')
this.exitCode = 1
return
}
user.password = password
await user.save()
this.logger.success('Mot de passe changé.')
}
}