52 lines
1.7 KiB
TypeScript
52 lines
1.7 KiB
TypeScript
import { BaseCommand } from '@adonisjs/core/ace'
|
|
import type { CommandOptions } from '@adonisjs/core/types/ace'
|
|
|
|
/**
|
|
* Creates the account used to log in. There is no sign-up page: the only
|
|
* way to create an account is this command, run on the VPS:
|
|
*
|
|
* docker exec -it big-brother node ace user:create
|
|
*/
|
|
export default class UserCreate extends BaseCommand {
|
|
static commandName = 'user:create'
|
|
static description = 'Créer un compte (mot de passe + double authentification)'
|
|
|
|
static options: CommandOptions = {
|
|
startApp: true,
|
|
}
|
|
|
|
async run() {
|
|
const { default: User } = await import('#models/user')
|
|
const { enrollTotp } = await import('#services/totp_enrollment')
|
|
|
|
const username = await this.prompt.ask('Identifiant', {
|
|
validate: (value) =>
|
|
/^[\w.-]{3,64}$/.test(value) || '3 à 64 caractères : lettres, chiffres, . _ -',
|
|
})
|
|
if (await User.findBy('username', username)) {
|
|
this.logger.error(`Le compte « ${username} » existe déjà.`)
|
|
this.exitCode = 1
|
|
return
|
|
}
|
|
|
|
const password = await this.prompt.secure('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
|
|
}
|
|
|
|
const totpSecret = await enrollTotp(this, username)
|
|
if (!totpSecret) {
|
|
this.exitCode = 1
|
|
return
|
|
}
|
|
|
|
await User.create({ username, password, totpSecret })
|
|
this.logger.success(`Compte « ${username} » créé. Tu peux te connecter.`)
|
|
}
|
|
}
|