@@ -4,6 +4,9 @@ import { loginSchema, registerSchema } from "./auth.validation";
4
4
import { AuthService } from "./auth.service" ;
5
5
import { Controller } from "interfaces/controller.interface" ;
6
6
import 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" ;
7
10
8
11
export class AuthController extends Controller {
9
12
private authService = new AuthService ( ) ;
@@ -23,7 +26,7 @@ export class AuthController extends Controller {
23
26
private register = async ( request : Request , response : Response , next : NextFunction ) => {
24
27
try {
25
28
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" ) ;
27
30
28
31
response . cookie (
29
32
"Authentication" ,
@@ -40,7 +43,7 @@ export class AuthController extends Controller {
40
43
private login = async ( request : Request , response : Response , next : NextFunction ) => {
41
44
try {
42
45
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" ) ;
44
47
45
48
response . cookie (
46
49
"Authentication" ,
@@ -57,7 +60,7 @@ export class AuthController extends Controller {
57
60
private isLoggedIn = async ( request : Request , response : Response , next : NextFunction ) => {
58
61
try {
59
62
const loggedUser = await this . authService . isLoggedIn ( request . userId ) ;
60
- if ( ! loggedUser ) return next ( ) ;
63
+ if ( ! loggedUser ) throw new UnauthorizedError ( "User not logged in" ) ;
61
64
62
65
response . json ( loggedUser ) ;
63
66
} catch ( error ) {
0 commit comments