File size: 1,656 Bytes
093279d
677259a
 
6099975
 
2f0e1b7
 
 
 
a99a515
 
a6fbb8d
093279d
 
677259a
22f0230
ac9dbd5
 
1628025
093279d
f39e411
 
 
 
093279d
f39e411
1628025
 
a6fbb8d
677259a
484fdbc
3787f0d
484fdbc
f53e2dd
677259a
484fdbc
22f0230
a6fbb8d
484fdbc
677259a
484fdbc
f53e2dd
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
47
import { UsersAuthService } from "../services/users.service";
import { loginValidationSchema } from "../validation/login.validation";
import { Request, Response } from "express";
import { JsonResponse } from "@lib/responses/json-response";
import { userRegisterSchema, IUserRegister } from "@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.toJSON(), 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.toJSON(), UserSerialization), token },
    });
    return res.json(response);
  };
}