fix: Add email validation to admin user seeding process
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
/* eslint-disable no-console */
|
||||
|
||||
const { read } = require('read');
|
||||
const validator = require('validator');
|
||||
const initKnex = require('knex');
|
||||
|
||||
const knexfile = require('./knexfile');
|
||||
@@ -51,6 +52,21 @@ const input = async (fieldName, options = {}) => {
|
||||
isRequired: true,
|
||||
});
|
||||
|
||||
// eslint-disable-next-line no-constant-condition
|
||||
while (true) {
|
||||
if (
|
||||
validator.isEmail(process.env.DEFAULT_ADMIN_EMAIL) &&
|
||||
process.env.DEFAULT_ADMIN_EMAIL.length <= 256
|
||||
) {
|
||||
break;
|
||||
}
|
||||
|
||||
console.log('Email must be a valid e-mail address with no more than 256 characters.');
|
||||
process.env.DEFAULT_ADMIN_EMAIL = await input('Email', {
|
||||
isRequired: true,
|
||||
});
|
||||
}
|
||||
|
||||
process.env.DEFAULT_ADMIN_PASSWORD = await input('Password', {
|
||||
isRequired: true,
|
||||
isPassword: true,
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
/* eslint-disable no-console */
|
||||
|
||||
const bcrypt = require('bcrypt');
|
||||
const validator = require('validator');
|
||||
|
||||
const USERNAME_REGEX = /^[a-zA-Z0-9]+((_|\.)?[a-zA-Z0-9])*$/;
|
||||
|
||||
@@ -63,6 +64,18 @@ exports.seed = async (knex) => {
|
||||
process.env.DEFAULT_ADMIN_EMAIL && process.env.DEFAULT_ADMIN_EMAIL.toLowerCase();
|
||||
|
||||
if (defaultAdminEmail) {
|
||||
if (!validator.isEmail(defaultAdminEmail)) {
|
||||
throw new Error(
|
||||
`DEFAULT_ADMIN_EMAIL "${process.env.DEFAULT_ADMIN_EMAIL}" is not a valid e-mail address.`,
|
||||
);
|
||||
}
|
||||
|
||||
if (defaultAdminEmail.length > 256) {
|
||||
throw new Error(
|
||||
`DEFAULT_ADMIN_EMAIL "${process.env.DEFAULT_ADMIN_EMAIL}" exceeds 256 characters.`,
|
||||
);
|
||||
}
|
||||
|
||||
const userData = buildUserData();
|
||||
|
||||
let userId;
|
||||
|
||||
Reference in New Issue
Block a user