-
Notifications
You must be signed in to change notification settings - Fork 0
/
createAdminUserScript.ts
52 lines (40 loc) · 1.31 KB
/
createAdminUserScript.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import Connection from "./database/connection";
import dotenv from 'dotenv';
import bcrypt from 'bcrypt';
import Static from "./static";
dotenv.config();
interface IUser {
nickname: string;
email: string;
password: string;
createdAt?: string;
updatedAt?: string;
}
async function createAdminUser() {
const currentDate = new Static().getCurrentDate();
const admin: object = await Connection("users").select("*").where({ nickname: 'admin' }).first();
const ROUNDS = 10
const salt:string = bcrypt.genSaltSync(ROUNDS);
const hashedPassword = bcrypt.hashSync(process.env.ADMIN_PASSWORD!, salt)
if (admin !== undefined) {
return;
} else {
const newAdmin: IUser = {
nickname: process.env.NICKNAME!,
email: process.env.ADMIN_EMAIL!,
password: hashedPassword,
createdAt: currentDate,
updatedAt: currentDate
};
await Connection("users").insert(newAdmin);
await Connection("roles").insert({
role: 1,
user_id: 1,
description: "admin",
createdAt: currentDate,
updatedAt: currentDate
});
console.log("ADVICE: <===ADMIN CREATED!===>");
}
}
export default createAdminUser;