87 lines
2.2 KiB
JavaScript
87 lines
2.2 KiB
JavaScript
|
// @ts-check
|
||
|
|
||
|
const fs = require("fs");
|
||
|
const path = require("path");
|
||
|
|
||
|
require("dotenv").config({ path: path.resolve(__dirname, ".env") });
|
||
|
const {
|
||
|
hashPassword,
|
||
|
} = require("./package-shared/functions/backend/passwordHash");
|
||
|
|
||
|
const SSL_DIR = "/app/ssl";
|
||
|
|
||
|
const mysql = require("serverless-mysql");
|
||
|
|
||
|
const connection = mysql({
|
||
|
config: {
|
||
|
host: process.env.DSQL_DB_HOST,
|
||
|
user: process.env.DSQL_DB_USERNAME,
|
||
|
password: process.env.DSQL_DB_PASSWORD,
|
||
|
database: process.env.DSQL_DB_NAME,
|
||
|
charset: "utf8mb4",
|
||
|
ssl: {
|
||
|
ca: fs.readFileSync(`${SSL_DIR}/ca-cert.pem`),
|
||
|
},
|
||
|
},
|
||
|
});
|
||
|
|
||
|
/**
|
||
|
* # Create Extra users
|
||
|
* @description Create `READ_ONLY` and `FULL_ACCESS` users
|
||
|
*/
|
||
|
async function initRootUser() {
|
||
|
/**
|
||
|
* Grab Schema
|
||
|
*
|
||
|
* @description Grab Schema
|
||
|
*/
|
||
|
const first_name = process.env.DSQL_LOCAL_SU_FIRST_NAME;
|
||
|
const last_name = process.env.DSQL_LOCAL_SU_LAST_NAME;
|
||
|
const email = process.env.DSQL_LOCAL_SU_EMAIL;
|
||
|
const username = process.env.DSQL_LOCAL_SU_USERNAME;
|
||
|
const password = process.env.DSQL_LOCAL_SU_PASSWORD;
|
||
|
|
||
|
if (!password) {
|
||
|
throw new Error("Please enter a root admin user password");
|
||
|
}
|
||
|
|
||
|
const hashedPassword = hashPassword(password);
|
||
|
|
||
|
const existingUser = await connection.query(
|
||
|
`SELECT * FROM users WHERE id = '1'`
|
||
|
);
|
||
|
|
||
|
/** @type {any} */
|
||
|
const userObject = {
|
||
|
id: 1,
|
||
|
first_name,
|
||
|
last_name,
|
||
|
username,
|
||
|
email,
|
||
|
password: hashedPassword,
|
||
|
verification_status: 1,
|
||
|
user_priviledge: 7,
|
||
|
date_created: Date(),
|
||
|
date_created_code: Date.now(),
|
||
|
date_updated: Date(),
|
||
|
date_updated_code: Date.now(),
|
||
|
};
|
||
|
|
||
|
if (existingUser?.[0]) {
|
||
|
delete userObject.id;
|
||
|
delete userObject.date_created;
|
||
|
delete userObject.date_created_code;
|
||
|
delete userObject.verification_status;
|
||
|
delete userObject.user_priviledge;
|
||
|
|
||
|
await connection.query(`UPDATE users SET ? WHERE id='1'`, userObject);
|
||
|
process.exit();
|
||
|
}
|
||
|
|
||
|
await connection.query(`INSERT INTO users SET ?`, userObject);
|
||
|
|
||
|
process.exit();
|
||
|
}
|
||
|
|
||
|
initRootUser();
|