diff --git a/api/src/cli/commands/users/passwd.ts b/api/src/cli/commands/users/passwd.ts new file mode 100644 index 0000000000..d2909aa598 --- /dev/null +++ b/api/src/cli/commands/users/passwd.ts @@ -0,0 +1,32 @@ +import argon2 from 'argon2'; +import { getSchema } from '../../../utils/get-schema'; + +export default async function usersPasswd({ email, password }: any) { + const { default: database } = require('../../../database/index'); + const { UsersService } = require('../../../services/users'); + + if (!email || !password) { + console.error('Email and password are required'); + process.exit(1); + } + + try { + const passwordHashed = await argon2.hash(password); + const schema = await getSchema(); + const service = new UsersService({ schema, knex: database }); + + const user = await service.knex.select('id').from('directus_users').where({ email }).first(); + if (user) { + await service.knex('directus_users').update({ password: passwordHashed }).where({ id: user.id }); + console.log(`Password is updated for user ${user.id}`); + } else { + console.log('No such user by this email'); + } + + await database.destroy(); + process.exit(user ? 0 : 1); + } catch (err) { + console.error(err); + process.exit(1); + } +} diff --git a/api/src/cli/index.ts b/api/src/cli/index.ts index e44b179212..48206eabd5 100644 --- a/api/src/cli/index.ts +++ b/api/src/cli/index.ts @@ -9,6 +9,7 @@ import init from './commands/init'; import dbInstall from './commands/database/install'; import dbMigrate from './commands/database/migrate'; import usersCreate from './commands/users/create'; +import usersPasswd from './commands/users/passwd'; import rolesCreate from './commands/roles/create'; import count from './commands/count'; import bootstrap from './commands/bootstrap'; @@ -42,6 +43,12 @@ usersCommand .option('--password ', `user's password`) .option('--role ', `user's role`) .action(usersCreate); +usersCommand + .command('passwd') + .description('Set user password') + .option('--email ', `user's email`) + .option('--password ', `user's new password`) + .action(usersPasswd); const rolesCommand = program.command('roles'); rolesCommand