preapring for rls
This commit is contained in:
52
scripts/create-admin.mjs
Normal file
52
scripts/create-admin.mjs
Normal file
@@ -0,0 +1,52 @@
|
||||
import db from "../src/inc/sql.mjs";
|
||||
import lib from "../src/inc/lib.mjs";
|
||||
import cfg from "../src/inc/config.mjs";
|
||||
import { getDefaultLayout } from "../src/inc/settings.mjs";
|
||||
|
||||
const [username, password] = process.argv.slice(2);
|
||||
|
||||
if (!username || !password) {
|
||||
console.error("Usage: node scripts/create-admin.mjs <username> <password>");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (password.length < 20) {
|
||||
console.error("Error: Password must be at least 20 characters long to meet system security requirements.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
async function createAdmin() {
|
||||
console.log(`--- Creating Admin User: ${username} ---`);
|
||||
|
||||
// Check if user exists
|
||||
const existing = await db`select id from "user" where "login" = ${username.toLowerCase()} or "user" = ${username}`;
|
||||
if (existing.length > 0) {
|
||||
console.error("Error: Username already taken.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const hash = await lib.hash(password);
|
||||
const ts = ~~(Date.now() / 1e3);
|
||||
|
||||
try {
|
||||
const newUser = await db`
|
||||
insert into "user" ("login", "password", "user", "created_at", "admin", "is_moderator", "activated")
|
||||
values (${username.toLowerCase()}, ${hash}, ${username}, to_timestamp(${ts}), true, true, true)
|
||||
returning id
|
||||
`;
|
||||
const userId = newUser[0].id;
|
||||
|
||||
await db`
|
||||
insert into user_options (user_id, mode, theme, fullscreen, avatar, avatar_file, use_new_layout, disable_autoplay, disable_swiping)
|
||||
values (${userId}, 3, 'amoled', 0, null, 'default.png', ${getDefaultLayout() === 'modern'}, false, false)
|
||||
`;
|
||||
|
||||
console.log(`--- Admin User ${username} Created Successfully ---`);
|
||||
process.exit(0);
|
||||
} catch (err) {
|
||||
console.error("Error creating admin user:", err);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
createAdmin();
|
||||
Reference in New Issue
Block a user