fix: Add username uniqueness check to admin user seeding

This commit is contained in:
Ayman
2026-07-08 07:17:32 +01:00
parent 0a2641db84
commit 7fbc2ea737
2 changed files with 23 additions and 5 deletions
+13 -5
View File
@@ -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');
+10
View File
@@ -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(