-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateStrapiSuperAdmin.js
55 lines (54 loc) · 1.45 KB
/
createStrapiSuperAdmin.js
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
53
54
55
const createStrapiSuperAdmin = async (
strapiInstance,
email = "[email protected]",
password = "test",
username = "test",
firstname = "test",
lastname = "test",
blocked = false,
isActive = true,
displayLogs = true // print success logs
) => {
const params = {
username,
password,
firstname,
lastname,
email,
blocked,
isActive,
};
let verifyRole = await strapiInstance
.query("role", "admin")
.findOne({ code: "strapi-super-admin" });
if (!verifyRole) {
verifyRole = await strapiInstance.query("role", "admin").create({
name: "Super Admin",
code: "strapi-super-admin",
description:
"Super Admins can access and manage all features and settings.",
});
}
params.roles = [verifyRole.id];
let tmp_pass = params.password;
params.password = await strapiInstance.admin.services.auth.hashPassword(
params.password
);
try {
await strapiInstance.query("user", "admin").create({
...params,
});
if (displayLogs) {
strapiInstance.log.info("Admin account was successfully created.");
strapiInstance.log.info(`Email: ${params.email}`);
strapiInstance.log.info(`Password: ${tmp_pass}`);
}
} catch (error) {
strapiInstance.log.error(
`Couldn't create Admin account during bootstrap: `,
error
);
console.error(`Couldn't create Admin account during bootstrap: `, error);
}
};
module.exports = { createStrapiSuperAdmin };