refactor(register): split the code to be more standard to the rest of the app
This commit is contained in:
@@ -1 +1,46 @@
|
|||||||
export const registerUser = async () => {};
|
import {eq} from "drizzle-orm";
|
||||||
|
import {db} from "../../../../database/dbclient.js";
|
||||||
|
import {users} from "../../../../database/schema/users.js";
|
||||||
|
import {createPassword} from "../utils/createPassword.js";
|
||||||
|
import {setSysAdmin} from "./userRoles/setSysAdmin.js";
|
||||||
|
|
||||||
|
export const registerUser = async (username: string, password: string, email: string) => {
|
||||||
|
const usercount = await db.select().from(users);
|
||||||
|
|
||||||
|
// make sure the user dose not already exist in the system
|
||||||
|
const userCheck = await db.select().from(users).where(eq(users.username, username));
|
||||||
|
|
||||||
|
if (userCheck.length === 1) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: `${username} already exists please login or reset password, if you feel this is an error please contact your admin.`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// make sure we only send over a username that is all lowercase
|
||||||
|
username = username.toLowerCase();
|
||||||
|
|
||||||
|
// get the good kinda password
|
||||||
|
password = await createPassword(password);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const user = await db
|
||||||
|
.insert(users)
|
||||||
|
.values({username, email, password})
|
||||||
|
.returning({user: users.username, email: users.email});
|
||||||
|
|
||||||
|
if (usercount.length <= 1) {
|
||||||
|
console.log(`${username} is the first user and will be set to system admin.`);
|
||||||
|
const updateUser = await db.select().from(users).where(eq(users.username, username));
|
||||||
|
setSysAdmin(updateUser, "systemAdmin");
|
||||||
|
}
|
||||||
|
|
||||||
|
return {sucess: true, message: "User Registered", user};
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: `${username} already exists please login or reset password, if you feel this is an error please contact your admin.`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
import {z, createRoute, OpenAPIHono} from "@hono/zod-openapi";
|
import {z, createRoute, OpenAPIHono} from "@hono/zod-openapi";
|
||||||
import {db} from "../../../../database/dbclient.js";
|
|
||||||
import {users} from "../../../../database/schema/users.js";
|
|
||||||
import {apiHit} from "../../../globalUtils/apiHits.js";
|
import {apiHit} from "../../../globalUtils/apiHits.js";
|
||||||
import {createPassword} from "../utils/createPassword.js";
|
import {registerUser} from "../controllers/register.js";
|
||||||
import {eq} from "drizzle-orm";
|
|
||||||
|
|
||||||
const app = new OpenAPIHono();
|
const app = new OpenAPIHono();
|
||||||
|
|
||||||
@@ -24,6 +21,7 @@ const UserSchema = z.object({
|
|||||||
type User = z.infer<typeof UserSchema>;
|
type User = z.infer<typeof UserSchema>;
|
||||||
|
|
||||||
const responseSchema = z.object({
|
const responseSchema = z.object({
|
||||||
|
success: z.boolean().optional().openapi({example: true}),
|
||||||
message: z.string().optional().openapi({example: "User Created"}),
|
message: z.string().optional().openapi({example: "User Created"}),
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -79,32 +77,10 @@ app.openapi(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// make sure the user dose not already exist in the system
|
|
||||||
const userCheck = await db.select().from(users).where(eq(users.username, username));
|
|
||||||
|
|
||||||
if (userCheck.length === 1) {
|
|
||||||
return c.json(
|
|
||||||
{
|
|
||||||
success: false,
|
|
||||||
message: `${username} already exists please login or reset password, if you feel this is an error please contact your admin.`,
|
|
||||||
},
|
|
||||||
400
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// make sure we only send over a username that is all lowercase
|
|
||||||
username = username.toLowerCase();
|
|
||||||
|
|
||||||
// get the good kinda password
|
|
||||||
password = await createPassword(password);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const user = await db
|
const register = await registerUser(username, password, email);
|
||||||
.insert(users)
|
|
||||||
.values({username, email, password})
|
|
||||||
.returning({user: users.username, email: users.email});
|
|
||||||
|
|
||||||
return c.json({message: "User Registered", user}, 200);
|
return c.json({success: register.success, message: register.message, user: register?.user}, 200);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
return c.json(
|
return c.json(
|
||||||
|
|||||||
Reference in New Issue
Block a user