diff --git a/src/controllers/auth.controller.ts b/src/controllers/auth.controller.ts index e90670a..01fb326 100644 --- a/src/controllers/auth.controller.ts +++ b/src/controllers/auth.controller.ts @@ -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) { diff --git a/src/services/auth.service.ts b/src/services/auth.service.ts index cd1fe68..730e791 100644 --- a/src/services/auth.service.ts +++ b/src/services/auth.service.ts @@ -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 }; diff --git a/src/validation/auth.schema.ts b/src/validation/auth.schema.ts index 433bdee..3ea8d0c 100644 --- a/src/validation/auth.schema.ts +++ b/src/validation/auth.schema.ts @@ -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"), }), });