umami/scripts/change-password.js

95 lines
2.0 KiB
JavaScript
Raw Normal View History

2022-08-29 05:20:54 +02:00
/* eslint-disable no-console */
require('dotenv').config();
2022-08-29 05:20:54 +02:00
const { hashPassword } = require('next-basics');
const chalk = require('chalk');
const prompts = require('prompts');
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
const runQuery = async query => {
return query.catch(e => {
throw e;
});
};
2022-11-01 07:42:37 +01:00
const updateUserByUsername = (username, data) => {
return runQuery(
2022-11-01 07:42:37 +01:00
prisma.user.update({
where: {
username,
},
data,
}),
);
};
const changePassword = async (username, newPassword) => {
const password = hashPassword(newPassword);
2022-11-01 07:42:37 +01:00
return updateUserByUsername(username, { password });
};
const getUsernameAndPassword = async () => {
let [username, password] = process.argv.slice(2);
if (username && password) {
return { username, password };
}
const questions = [];
if (!username) {
questions.push({
type: 'text',
name: 'username',
2022-11-01 07:42:37 +01:00
message: 'Enter user to change password',
});
}
if (!password) {
questions.push(
{
type: 'password',
name: 'password',
message: 'Enter new password',
},
{
type: 'password',
name: 'confirmation',
message: 'Confirm new password',
},
);
}
const answers = await prompts(questions);
if (answers.password !== answers.confirmation) {
throw new Error(`Passwords don't match`);
}
return {
username: username || answers.username,
password: answers.password,
};
};
(async () => {
let username, password;
try {
({ username, password } = await getUsernameAndPassword());
} catch (error) {
console.log(chalk.redBright(error.message));
return;
}
try {
await changePassword(username, password);
console.log('Password changed for user', chalk.greenBright(username));
} catch (error) {
2022-01-06 09:49:07 +01:00
if (error.meta.cause.includes('Record to update not found')) {
2022-11-01 07:42:37 +01:00
console.log('User not found:', chalk.redBright(username));
} else {
throw error;
}
}
prisma.$disconnect();
})();