Skip to content

Commit a943d93

Browse files
committed
update auth
1 parent ab78fc3 commit a943d93

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

src/auth/auth.controller.ts

-11
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import { loginSchema, registerSchema } from "./auth.validation";
44
import { AuthService } from "./auth.service";
55
import { Controller } from "interfaces/controller.interface";
66
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";
107

118
export class AuthController extends Controller {
129
private authService = new AuthService();
@@ -26,14 +23,11 @@ export class AuthController extends Controller {
2623
private register = async (request: Request, response: Response, next: NextFunction) => {
2724
try {
2825
const createdUser = await this.authService.registerUser(request.body);
29-
if (!createdUser) throw new BadRequestError("No user created");
30-
3126
response.cookie(
3227
"Authentication",
3328
this.authService.createToken(createdUser.id),
3429
this.authService.createCookieOptions()
3530
);
36-
3731
response.status(201).json(createdUser);
3832
} catch (error) {
3933
next(error);
@@ -43,14 +37,11 @@ export class AuthController extends Controller {
4337
private login = async (request: Request, response: Response, next: NextFunction) => {
4438
try {
4539
const user = await this.authService.login(request.body);
46-
if (!user) throw new NotFoundError("No user found");
47-
4840
response.cookie(
4941
"Authentication",
5042
this.authService.createToken(user.id),
5143
this.authService.createCookieOptions()
5244
);
53-
5445
response.json(user);
5546
} catch (error) {
5647
next(error);
@@ -60,8 +51,6 @@ export class AuthController extends Controller {
6051
private isLoggedIn = async (request: Request, response: Response, next: NextFunction) => {
6152
try {
6253
const loggedUser = await this.authService.isLoggedIn(request.userId);
63-
if (!loggedUser) throw new UnauthorizedError("User not logged in");
64-
6554
response.json(loggedUser);
6655
} catch (error) {
6756
next(error);

src/auth/auth.service.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { env } from "config/env";
66
import { BadRequestError } from "errors/bad-request.error";
77
import { CookieOptions } from "express";
88
import { User } from "users/users.model";
9+
import { NotFoundError } from "errors/not-found";
10+
import { UnauthorizedError } from "errors/unauthorized.error";
911

1012
export class AuthService {
1113
private authRepository = new AuthRepository();
@@ -43,8 +45,16 @@ export class AuthService {
4345
}
4446

4547
public async isLoggedIn(userId?: number) {
46-
if (!userId) return null;
47-
return this.authRepository.findUserById(userId);
48+
if (!userId) {
49+
throw new UnauthorizedError("User not logged in");
50+
}
51+
52+
const user = await this.authRepository.findUserById(userId);
53+
if (!user) {
54+
throw new NotFoundError("User not found");
55+
}
56+
57+
return this.removePassword(user);
4858
}
4959

5060
public createToken(userId: number) {

0 commit comments

Comments
 (0)