@@ -4,6 +4,9 @@ import { loginSchema, registerSchema } from "./auth.validation";
44import { AuthService } from "./auth.service" ;
55import { Controller } from "interfaces/controller.interface" ;
66import authMiddleware from "middleware/auth.middleware" ;
7+ import { UnauthorizedError } from "errors/unauthorized.error" ;
8+ import { NotFoundError } from "errors/not-found" ;
9+ import { BadRequestError } from "errors/bad-request.error" ;
710
811export class AuthController extends Controller {
912 private authService = new AuthService ( ) ;
@@ -23,7 +26,7 @@ export class AuthController extends Controller {
2326 private register = async ( request : Request , response : Response , next : NextFunction ) => {
2427 try {
2528 const createdUser = await this . authService . registerUser ( request . body ) ;
26- if ( ! createdUser ) return next ( "No user created" ) ;
29+ if ( ! createdUser ) throw new BadRequestError ( "No user created" ) ;
2730
2831 response . cookie (
2932 "Authentication" ,
@@ -40,7 +43,7 @@ export class AuthController extends Controller {
4043 private login = async ( request : Request , response : Response , next : NextFunction ) => {
4144 try {
4245 const user = await this . authService . login ( request . body ) ;
43- if ( ! user ) return next ( "No user found" ) ;
46+ if ( ! user ) throw new NotFoundError ( "No user found" ) ;
4447
4548 response . cookie (
4649 "Authentication" ,
@@ -57,7 +60,7 @@ export class AuthController extends Controller {
5760 private isLoggedIn = async ( request : Request , response : Response , next : NextFunction ) => {
5861 try {
5962 const loggedUser = await this . authService . isLoggedIn ( request . userId ) ;
60- if ( ! loggedUser ) return next ( ) ;
63+ if ( ! loggedUser ) throw new UnauthorizedError ( "User not logged in" ) ;
6164
6265 response . json ( loggedUser ) ;
6366 } catch ( error ) {
0 commit comments