File size: 2,155 Bytes
2c16c8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
import { Request, Response } from "express";
import { AuthService } from "./auth.service";
import {
  loginRequestSchema,
  refreshSessionRequestSchema,
  registerUserRequestSchema,
  resendOtpRequestSchema,
  resetPasswordRequestSchema,
  verifyOtpRequestSchema
} from "./auth.dto";
import { Validate } from "@/api/middleware/validation.middleware";
import { RequestHandler } from "@/shared/http/request-handler";
import { HttpStatusCode } from "@/shared/http/status-code";

export class AuthController {
  constructor(private readonly authService: AuthService) {}

  @Validate({ body: registerUserRequestSchema })
  public async register(req: Request, res: Response){
    return RequestHandler.handle(() => this.authService.register(req.body), res);
  }

  @Validate({ body: loginRequestSchema })
  public async login(req: Request, res: Response){
    return RequestHandler.handle(() => this.authService.login(req.body), res);
  }

  @Validate({ body: verifyOtpRequestSchema })
  public async verifyOtp(req: Request, res: Response) {
    return RequestHandler.handle(() => this.authService.verifyEmailOtp(req.body), res);
  }

  @Validate({ body: resendOtpRequestSchema })
  public async resendOtp(req: Request, res: Response) {
    return RequestHandler.handle(() => this.authService.resendOtp(req.body), res);
  }

  @Validate({ body: resetPasswordRequestSchema })
  public async resetPassword(req: Request, res: Response) {
    return RequestHandler.handle(() => this.authService.resetPassword(req.body), res);
  }

  @Validate({body: refreshSessionRequestSchema})
  public async refreshSession(req: Request, res: Response) {
    return RequestHandler.handle(() => this.authService.refreshSession(req.body), res);
  }

  public async forgetPassword(req: Request, res: Response) {
    const email = req.params.email;

    if (!email) {
      return res.error(HttpStatusCode.BAD_REQUEST, 'VALIDATION_ERROR', 'Invalid params', [
        { message: 'Email is required', field: 'email' }
      ]);
    }

    return RequestHandler.handle(() => this.authService.forgetPassword(email), res);
  }
}