fix: Add username uniqueness check to admin user seeding
This commit is contained in:
@@ -97,12 +97,20 @@ const input = async (fieldName, options = {}) => {
|
|||||||
USERNAME_REGEX.test(process.env.DEFAULT_ADMIN_USERNAME);
|
USERNAME_REGEX.test(process.env.DEFAULT_ADMIN_USERNAME);
|
||||||
|
|
||||||
if (isValid) {
|
if (isValid) {
|
||||||
break;
|
const existingUser = await knex('user_account')
|
||||||
}
|
.where('username', process.env.DEFAULT_ADMIN_USERNAME.toLowerCase())
|
||||||
|
.first();
|
||||||
|
|
||||||
console.log(
|
if (!existingUser) {
|
||||||
'Username must be 3-32 characters and contain only letters, digits, underscores, and dots (e.g., john_doe).',
|
break;
|
||||||
);
|
}
|
||||||
|
|
||||||
|
console.log('Username is already in use.');
|
||||||
|
} else {
|
||||||
|
console.log(
|
||||||
|
'Username must be 3-32 characters and contain only letters, digits, underscores, and dots (e.g., john_doe).',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
process.env.DEFAULT_ADMIN_USERNAME = await input('Username');
|
process.env.DEFAULT_ADMIN_USERNAME = await input('Username');
|
||||||
|
|
||||||
|
|||||||
@@ -78,6 +78,16 @@ exports.seed = async (knex) => {
|
|||||||
|
|
||||||
const userData = buildUserData();
|
const userData = buildUserData();
|
||||||
|
|
||||||
|
if (userData.username) {
|
||||||
|
const existingUsernameUser = await knex('user_account')
|
||||||
|
.where('username', userData.username)
|
||||||
|
.first();
|
||||||
|
|
||||||
|
if (existingUsernameUser) {
|
||||||
|
throw new Error(`User with DEFAULT_ADMIN_USERNAME "${userData.username}" already exists.`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let userId;
|
let userId;
|
||||||
try {
|
try {
|
||||||
[{ id: userId }] = await knex('user_account').insert(
|
[{ id: userId }] = await knex('user_account').insert(
|
||||||
|
|||||||
Reference in New Issue
Block a user