added role to register

This commit is contained in:
2026-01-13 17:56:43 +03:00
parent 5b08104492
commit d2334181a0
3 changed files with 6 additions and 4 deletions
+2 -2
View File
@@ -4,9 +4,9 @@ import { AuthService } from "../services/auth.service.js";
export class AuthController {
static async register(req: Request, res: Response, next: NextFunction) {
try {
const { email, password } = req.body;
const { email, password, role } = req.body;
const user = await AuthService.register(email, password);
const user = await AuthService.register(email, password, role);
res.status(201).json(user);
} catch (err) {
+3 -2
View File
@@ -1,9 +1,10 @@
import { UserRepository } from "../repositories/user.repo.js";
import { hashPassword, verifyPassword } from "../utils/password.js";
import { signToken } from "../utils/jwt.js";
import type { UserRole } from "../models/user.model.js";
export class AuthService {
static async register(email: string, password: string) {
static async register(email: string, password: string, role: UserRole = "customer") {
const existing = await UserRepository.findByEmail(email);
if (existing) {
throw { statusCode: 409, message: "Email already registered" };
@@ -14,7 +15,7 @@ export class AuthService {
const user = await UserRepository.create({
email,
passwordHash,
role: "customer",
role,
});
return { id: user._id, email: user.email, role: user.role };
+1
View File
@@ -4,6 +4,7 @@ export const registerSchema = z.object({
body: z.object({
email: z.string().email(),
password: z.string().min(8),
role: z.enum(["customer", "admin"]).default("customer"),
}),
});