mirror of
https://github.com/andwati/order-service.git
synced 2026-10-18 02:12:53 +00:00
added role to register
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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 };
|
||||
|
||||
@@ -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"),
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user