auth.controller.ts60 lines, 1.7 KB
| 1 | import { Request, Response, NextFunction } from "express"; |
| 2 | import { AuthService } from "../services/auth.service"; |
| 3 | import { successResponse } from "../utils/response"; |
| 4 | import { StatusCodes } from "http-status-codes"; |
| 5 | import { AppError } from "../utils/errors"; |
| 6 | |
| 7 | export class AuthController { |
| 8 | static async register(req: Request, res: Response, next: NextFunction) { |
| 9 | try { |
| 10 | const { email, password, name } = req.body; |
| 11 | const result = await AuthService.register(email, password, name); |
| 12 | res.status(StatusCodes.CREATED).json(successResponse(result)); |
| 13 | } catch (error) { |
| 14 | next(error); |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | static async login(req: Request, res: Response, next: NextFunction) { |
| 19 | try { |
| 20 | const { email, password } = req.body; |
| 21 | const result = await AuthService.login(email, password); |
| 22 | res.json(successResponse(result)); |
| 23 | } catch (error) { |
| 24 | next(error); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | static async verifyEmail(req: Request, res: Response, next: NextFunction) { |
| 29 | try { |
| 30 | const { token } = req.query; |
| 31 | const result = await AuthService.verifyEmail(token as string); |
| 32 | res.json(successResponse(result)); |
| 33 | } catch (error) { |
| 34 | next(error); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | static async resendVerificationEmail( |
| 39 | req: Request, |
| 40 | res: Response, |
| 41 | next: NextFunction |
| 42 | ) { |
| 43 | try { |
| 44 | const { email } = req.body; |
| 45 | const result = await AuthService.resendVerificationEmail(email); |
| 46 | res.json(successResponse(result)); |
| 47 | } catch (error) { |
| 48 | next(error); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | static async getProfile(req: Request, res: Response, next: NextFunction) { |
| 53 | try { |
| 54 | const result = await AuthService.getProfile(req.user?.userId!); |
| 55 | res.json(successResponse(result)); |
| 56 | } catch (error) { |
| 57 | next(error); |
| 58 | } |
| 59 | } |
| 60 | } |