Add non admin register
This commit is contained in:
parent
2d099120fa
commit
98957f8df1
|
|
@ -1,8 +1,8 @@
|
|||
import BaseError from "@/core/error/BaseError";
|
||||
import applicationJsonOnly from "@/core/utils/applicationJsonOnly";
|
||||
import handleCatchApi from "@/core/utils/handleCatchApi";
|
||||
import nonAdminRegisterAction from "@/modules/auth/actions/nonAdminRegisterAction";
|
||||
import { createUserSchema } from "@/modules/auth/formSchemas/CreateUserFormSchema";
|
||||
import createUser from "@/modules/auth/services/createUser";
|
||||
import mapObjectToFirstValue from "@/utils/mapObjectToFirstValue";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
|
|
@ -24,11 +24,10 @@ export async function POST(request: NextRequest) {
|
|||
});
|
||||
}
|
||||
|
||||
const result = await createUser({
|
||||
const result = await nonAdminRegisterAction({
|
||||
email: data.data.email,
|
||||
name: data.data.name,
|
||||
password: data.data.password,
|
||||
passwordConfirmation: data.data.passwordConfirmation ?? "",
|
||||
});
|
||||
|
||||
return NextResponse.json(result);
|
||||
|
|
|
|||
27
src/modules/auth/actions/nonAdminRegisterAction.ts
Normal file
27
src/modules/auth/actions/nonAdminRegisterAction.ts
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
"use server";
|
||||
import { cookies } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
import ServerResponseAction from "@/modules/dashboard/types/ServerResponseAction";
|
||||
import handleCatch from "@/modules/dashboard/utils/handleCatch";
|
||||
import nonAdminRegisterFormType from "../types/NonAdminRegisterFormType";
|
||||
import nonAdminRegister from "../services/nonAdminRegister";
|
||||
|
||||
/**
|
||||
* Creates a new user in the system.
|
||||
*
|
||||
* @param formData - The form data containing user details.
|
||||
* @returns An object indicating the result of the operation.
|
||||
*/
|
||||
export default async function nonAdminRegisterAction(
|
||||
formData: nonAdminRegisterFormType
|
||||
): Promise<ServerResponseAction> {
|
||||
//TODO: Add Throttling
|
||||
|
||||
try {
|
||||
const result = await nonAdminRegister(formData);
|
||||
cookies().set("token", result.token);
|
||||
redirect("/dashboard");
|
||||
} catch (e) {
|
||||
return handleCatch(e);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,3 @@
|
|||
import { cookies } from "next/headers";
|
||||
import getUserFromToken from "../utils/getUserFromToken";
|
||||
import AuthError from "../error/AuthError";
|
||||
|
||||
|
|
|
|||
38
src/modules/auth/services/nonAdminRegister.ts
Normal file
38
src/modules/auth/services/nonAdminRegister.ts
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
import unauthorized from "@/modules/dashboard/utils/unauthorized";
|
||||
import checkPermission from "../utils/checkPermission";
|
||||
import nonAdminRegisterFormType from "../types/NonAdminRegisterFormType";
|
||||
import db from "@/core/db";
|
||||
import AuthError from "../error/AuthError";
|
||||
import hashPassword from "../utils/hashPassword";
|
||||
import "server-only";
|
||||
import { createJwtToken } from "../utils/createJwtToken";
|
||||
|
||||
export default async function nonAdminRegister(data: nonAdminRegisterFormType) {
|
||||
if (!(await checkPermission("guest-only"))) unauthorized();
|
||||
|
||||
const existingUser = await db.user.findFirst({
|
||||
where: {
|
||||
email: data.email,
|
||||
},
|
||||
});
|
||||
|
||||
if (existingUser)
|
||||
throw new AuthError({
|
||||
errorCode: "USER_ALREADY_EXISTS",
|
||||
message:
|
||||
'This email has been registered before. If you forgot your password, you can click "Forgot Password"',
|
||||
statusCode: 401,
|
||||
});
|
||||
|
||||
const user = await db.user.create({
|
||||
data: {
|
||||
email: data.email,
|
||||
name: data.name,
|
||||
passwordHash: await hashPassword(data.password),
|
||||
},
|
||||
});
|
||||
|
||||
const token = createJwtToken({id: user.id})
|
||||
|
||||
return {user, token };
|
||||
}
|
||||
5
src/modules/auth/types/NonAdminRegisterFormType.d.ts
vendored
Normal file
5
src/modules/auth/types/NonAdminRegisterFormType.d.ts
vendored
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
export default interface nonAdminRegisterFormType {
|
||||
name: string,
|
||||
email: string,
|
||||
password: string,
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user