feat(lstv2 move): moved lstv2 into this app to keep them combined and easier to maintain
This commit is contained in:
56
lstV2/server/services/auth/utils/DefaultAccountCreation.ts
Normal file
56
lstV2/server/services/auth/utils/DefaultAccountCreation.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { db } from "../../../../database/dbclient.js";
|
||||
import { users } from "../../../../database/schema/users.js";
|
||||
import { tryCatch } from "../../../globalUtils/tryCatch.js";
|
||||
import { createLog } from "../../logger/logger.js";
|
||||
import { setSysAdmin } from "../controllers/userRoles/setSysAdmin.js";
|
||||
import { createPassword } from "./createPassword.js";
|
||||
|
||||
export const massAccountCreation = async () => {
|
||||
/**
|
||||
* This will create a new account for all users before if they are already in there it will update just there password.
|
||||
*
|
||||
*/
|
||||
|
||||
const user: any = [
|
||||
// {
|
||||
// username: "landa002",
|
||||
// email: "Oscar.Landa@alpla.com",
|
||||
// password: "Frostlike-Petri5-Ungreased!",
|
||||
// },
|
||||
];
|
||||
|
||||
for (let i = 0; i < user.length; i++) {
|
||||
const updatedUser = {
|
||||
username: user[i].username,
|
||||
email: user[i].email,
|
||||
password: await createPassword(user[i].password),
|
||||
};
|
||||
const { data, error } = await tryCatch(
|
||||
db
|
||||
.insert(users)
|
||||
.values(updatedUser)
|
||||
.onConflictDoUpdate({
|
||||
target: users.username,
|
||||
set: {
|
||||
password: updatedUser.password,
|
||||
email: updatedUser.email,
|
||||
},
|
||||
})
|
||||
.returning({
|
||||
user_id: users.user_id,
|
||||
username: users.username,
|
||||
})
|
||||
);
|
||||
|
||||
await setSysAdmin(data, "systemAdmin");
|
||||
|
||||
if (error) {
|
||||
createLog(
|
||||
"error",
|
||||
"lst",
|
||||
"auth",
|
||||
`There was an error creating ${user[i].username}`
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
18
lstV2/server/services/auth/utils/checkPassword.ts
Normal file
18
lstV2/server/services/auth/utils/checkPassword.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import bcrypt from "bcryptjs";
|
||||
export const checkPassword = async (
|
||||
currentPassword: string,
|
||||
dbPassword: string
|
||||
) => {
|
||||
let decyptPass = "";
|
||||
try {
|
||||
decyptPass = atob(dbPassword);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
// encypt password
|
||||
const pass: string | undefined = process.env.SECRET;
|
||||
|
||||
const checked = bcrypt.compareSync(pass + currentPassword, decyptPass);
|
||||
|
||||
return checked;
|
||||
};
|
||||
17
lstV2/server/services/auth/utils/createPassword.ts
Normal file
17
lstV2/server/services/auth/utils/createPassword.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import bcrypt from "bcryptjs";
|
||||
|
||||
export const createPassword = async (password: string) => {
|
||||
// encypt password
|
||||
let pass: string | undefined = process.env.SECRET;
|
||||
let salt: string | undefined = process.env.SALTING;
|
||||
|
||||
if (!pass || !salt) {
|
||||
pass = "error";
|
||||
} else {
|
||||
pass = bcrypt.hashSync(pass + password, parseInt(salt));
|
||||
|
||||
pass = btoa(pass);
|
||||
}
|
||||
|
||||
return pass;
|
||||
};
|
||||
44
lstV2/server/services/auth/utils/roleCheck.ts
Normal file
44
lstV2/server/services/auth/utils/roleCheck.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* check if the roles are in and if not add them.
|
||||
* this will only run on a server start up
|
||||
*/
|
||||
|
||||
import {db} from "../../../../database/dbclient.js";
|
||||
import {roles} from "../../../../database/schema/roles.js";
|
||||
import {createLog} from "../../logger/logger.js";
|
||||
// "view", "technician", "supervisor","manager", "admin", "systemAdmin"
|
||||
const newRoles = [
|
||||
{name: "viewer"},
|
||||
{name: "technician"},
|
||||
{name: "supervisor"},
|
||||
{name: "manager"},
|
||||
{name: "admin"},
|
||||
{name: "tester"},
|
||||
{name: "systemAdmin"},
|
||||
];
|
||||
export const areRolesIn = async () => {
|
||||
// get the roles
|
||||
try {
|
||||
const roleCheck = await db.select().from(roles);
|
||||
|
||||
if (roleCheck.length !== newRoles.length) {
|
||||
try {
|
||||
const newRole = await db
|
||||
.insert(roles)
|
||||
.values(newRoles)
|
||||
.onConflictDoNothing() // this will only update the ones that are new :D
|
||||
.returning({name: roles.name});
|
||||
createLog(
|
||||
"info",
|
||||
"lst",
|
||||
"auth",
|
||||
`${JSON.stringify(newRole)}, "Roles were just added due to missing them on server startup"`
|
||||
);
|
||||
} catch (error) {
|
||||
createLog("error", "lst", "auth", `There was an error adding new roles to the db, ${error}`);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
createLog("error", "lst", "auth", `There was an error adding new roles to the db, ${error}`);
|
||||
}
|
||||
};
|
||||
0
lstV2/server/services/auth/utils/verifyToken.ts
Normal file
0
lstV2/server/services/auth/utils/verifyToken.ts
Normal file
Reference in New Issue
Block a user