Added optional default admin account

This commit is contained in:
2021-03-07 06:59:58 +11:00
parent aacd64a769
commit ac980426a5
7 changed files with 54 additions and 4 deletions
+37
View File
@@ -0,0 +1,37 @@
//DOCS: ensure that there is at least one administration account
const bcrypt = require('bcryptjs');
const sequelize = require('../database');
const { accounts } = require('../database/models');
module.exports = async () => {
await sequelize.sync(); //this whole file is just one big BUGFIX
//validate env variables
if (!process.env.ADMIN_DEFAULT_USERNAME || !process.env.ADMIN_DEFAULT_PASSWORD) {
//skip this if arguments are missing
return;
}
if (process.env.ADMIN_DEFAULT_PASSWORD && process.env.ADMIN_DEFAULT_PASSWORD.length < 8) {
console.warn('ADMIN_DEFAULT_PASSWORD is too short - skipping default account creation');
return;
}
//check for an existing admin account
const adminRecord = await accounts.findOne({
where: {
privilege: 'administrator'
}
});
if (adminRecord == null) {
await accounts.create({
privilege: 'administrator',
email: `${process.env.ADMIN_DEFAULT_USERNAME}@${process.env.WEB_ADDRESS}`,
username: `${process.env.ADMIN_DEFAULT_USERNAME}`,
hash: await bcrypt.hash(`${process.env.ADMIN_DEFAULT_PASSWORD}`, await bcrypt.genSalt(11))
});
console.warn(`Created default admin account (email: ${process.env.ADMIN_DEFAULT_USERNAME}@${process.env.WEB_ADDRESS}; password: ${process.env.ADMIN_DEFAULT_PASSWORD})`);
}
};