Spaces:
Running
Running
File size: 1,643 Bytes
093279d 677259a 484fdbc 2f0e1b7 a99a515 a6fbb8d 093279d 677259a 22f0230 ac9dbd5 1628025 093279d f39e411 093279d f39e411 1628025 a6fbb8d 677259a 484fdbc a99a515 677259a 484fdbc 22f0230 a6fbb8d 484fdbc 677259a 484fdbc a99a515 677259a 484fdbc 22f0230 1628025 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
import { UsersAuthService } from "../services/users.service";
import { loginValidationSchema } from "../validation/login.validation";
import { Request, Response } from "express";
import { JsonResponse } from "src/lib/responses/json-response";
import { userRegisterSchema, IUserRegister } from "src/common/validations/user-register.validation";
import { asyncHandler } from "@helpers/async-handler";
import { bodyValidator } from "@helpers/validation.helper";
import { BaseController } from "@lib/controllers/controller.base";
import { Prefix } from "@lib/decorators/prefix.decorator";
import { serialize } from "@helpers/serialize";
import { UserSerialization } from "@common/serializers/user.serializtion";
@Prefix("/users/auth")
export class UsersAuthController extends BaseController {
private authService = new UsersAuthService();
setRoutes(): void {
this.router.post(
"/register",
bodyValidator(userRegisterSchema),
asyncHandler(this.register)
);
this.router.post(
"/login",
bodyValidator(loginValidationSchema),
asyncHandler(this.login)
);
}
register = async (req: Request, res: Response) => {
const user = await this.authService.register(req.body as IUserRegister);
const response = new JsonResponse({
data: serialize(user, UserSerialization),
});
return res.json(response);
};
login = async (req: Request, res: Response) => {
const { user, token } = await this.authService.login(req.body);
const response = new JsonResponse({
data: { user: serialize(user, UserSerialization), token },
});
return res.json(response);
};
}
|