Added optional default admin account
This commit is contained in:
@@ -13,5 +13,8 @@ MAIL_USERNAME=foobar@example.com
|
|||||||
MAIL_PASSWORD=examplepassword
|
MAIL_PASSWORD=examplepassword
|
||||||
MAIL_PHYSICAL=42 Placeholder Ave, Placeholder, 0000, USA
|
MAIL_PHYSICAL=42 Placeholder Ave, Placeholder, 0000, USA
|
||||||
|
|
||||||
|
ADMIN_DEFAULT_USERNAME=admin
|
||||||
|
ADMIN_DEFAULT_PASSWORD=password
|
||||||
|
|
||||||
SECRET_ACCESS=access
|
SECRET_ACCESS=access
|
||||||
SECRET_REFRESH=refresh
|
SECRET_REFRESH=refresh
|
||||||
|
|||||||
@@ -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})`);
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
module.exports = {
|
||||||
|
defaultAccount: require('./default-account')
|
||||||
|
};
|
||||||
@@ -51,10 +51,9 @@ const route = async (req, res) => {
|
|||||||
|
|
||||||
const validateDetails = async (body) => {
|
const validateDetails = async (body) => {
|
||||||
//basic formatting (with an exception for the default admin account)
|
//basic formatting (with an exception for the default admin account)
|
||||||
if (!validateEmail(body.email) && body.email != `admin@${process.env.WEB_ADDRESS}`) {
|
if (!validateEmail(body.email) && body.email != `${process.env.ADMIN_DEFAULT_USERNAME}@${process.env.WEB_ADDRESS}`) {
|
||||||
return 'invalid email';
|
return 'invalid email';
|
||||||
}
|
}
|
||||||
//TODO: restore default admin account
|
|
||||||
|
|
||||||
//check for existing (banned)
|
//check for existing (banned)
|
||||||
//TODO: restore banning
|
//TODO: restore banning
|
||||||
|
|||||||
@@ -78,6 +78,11 @@ const validateDetails = async (body) => {
|
|||||||
return 'username already exists';
|
return 'username already exists';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//validate password
|
||||||
|
if (body.password.length < 8) {
|
||||||
|
return 'password too short';
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -2,4 +2,4 @@ module.exports = {
|
|||||||
tokens: require('./tokens'),
|
tokens: require('./tokens'),
|
||||||
accounts: require('./accounts'),
|
accounts: require('./accounts'),
|
||||||
pendingSignups: require('./pending-signups')
|
pendingSignups: require('./pending-signups')
|
||||||
}
|
};
|
||||||
+4
-1
@@ -15,7 +15,10 @@ app.use(cors());
|
|||||||
//database connection
|
//database connection
|
||||||
const database = require('./database');
|
const database = require('./database');
|
||||||
|
|
||||||
//access the news
|
const admin = require('./admin');
|
||||||
|
admin.defaultAccount();
|
||||||
|
|
||||||
|
//access the auth
|
||||||
app.use('/auth', require('./auth'));
|
app.use('/auth', require('./auth'));
|
||||||
|
|
||||||
//error on access
|
//error on access
|
||||||
|
|||||||
Reference in New Issue
Block a user