mirror of
https://github.com/andwati/order-service.git
synced 2026-11-24 18:18:45 +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 {
|
export class AuthController {
|
||||||
static async register(req: Request, res: Response, next: NextFunction) {
|
static async register(req: Request, res: Response, next: NextFunction) {
|
||||||
try {
|
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);
|
res.status(201).json(user);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
import { UserRepository } from "../repositories/user.repo.js";
|
import { UserRepository } from "../repositories/user.repo.js";
|
||||||
import { hashPassword, verifyPassword } from "../utils/password.js";
|
import { hashPassword, verifyPassword } from "../utils/password.js";
|
||||||
import { signToken } from "../utils/jwt.js";
|
import { signToken } from "../utils/jwt.js";
|
||||||
|
import type { UserRole } from "../models/user.model.js";
|
||||||
|
|
||||||
export class AuthService {
|
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);
|
const existing = await UserRepository.findByEmail(email);
|
||||||
if (existing) {
|
if (existing) {
|
||||||
throw { statusCode: 409, message: "Email already registered" };
|
throw { statusCode: 409, message: "Email already registered" };
|
||||||
@@ -14,7 +15,7 @@ export class AuthService {
|
|||||||
const user = await UserRepository.create({
|
const user = await UserRepository.create({
|
||||||
email,
|
email,
|
||||||
passwordHash,
|
passwordHash,
|
||||||
role: "customer",
|
role,
|
||||||
});
|
});
|
||||||
|
|
||||||
return { id: user._id, email: user.email, role: user.role };
|
return { id: user._id, email: user.email, role: user.role };
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ export const registerSchema = z.object({
|
|||||||
body: z.object({
|
body: z.object({
|
||||||
email: z.string().email(),
|
email: z.string().email(),
|
||||||
password: z.string().min(8),
|
password: z.string().min(8),
|
||||||
|
role: z.enum(["customer", "admin"]).default("customer"),
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user