Skip to content

Commit 7c7e385

Browse files
committed
fix: auth middleware user object
1 parent a0edadd commit 7c7e385

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

src/user/auth.middleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class AuthMiddleware implements NestMiddleware {
2222
throw new HttpException('User not found.', HttpStatus.UNAUTHORIZED);
2323
}
2424

25-
req.user = user;
25+
req.user = user.user;
2626
next();
2727

2828
} else {

src/user/user.controller.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ export class UserController {
2121
constructor(private readonly userService: UserService) {}
2222

2323
@Get('user')
24-
async findMe(@User('id') userId: number): Promise<UserEntity> {
25-
return await this.userService.findById(userId);
24+
async findMe(@User('email') email: string): Promise<UserRO> {
25+
return await this.userService.findByEmail(email);
2626
}
2727

2828
@Put('user')
@@ -44,10 +44,10 @@ export class UserController {
4444
@UsePipes(new ValidationPipe())
4545
@Post('users/login')
4646
async login(@Body('user') loginUserDto: LoginUserDto): Promise<UserRO> {
47-
console.log("loginUserDto", loginUserDto);
4847
const _user = await this.userService.findOne(loginUserDto);
4948

50-
if (!_user) throw new HttpException('User not found.', 401);
49+
const errors = {User: ' not found'};
50+
if (!_user) throw new HttpException({errors}, 401);
5151

5252
const token = await this.userService.generateJWT(_user);
5353
const {email, username, bio, image} = _user;

src/user/user.decorator.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export const User = createRouteParamDecorator((data, req) => {
1111

1212
// in case a route is not protected, we still want to get the optional auth user from jwt
1313
const token = req.headers.authorization ? (req.headers.authorization as string).split(' ') : null;
14-
1514
if (token && token[1]) {
1615
const decoded: any = jwt.verify(token[1], SECRET);
1716
return !!data ? decoded[data] : decoded.user;

src/user/user.service.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,7 @@ export class UserService {
6161

6262
} else {
6363
const savedUser = await this.userRepository.save(newUser);
64-
const userRO = {
65-
username: savedUser.username,
66-
email: savedUser.email,
67-
bio: savedUser.bio,
68-
token: this.generateJWT(savedUser),
69-
image: savedUser.image
70-
};
71-
72-
return {user: userRO};
64+
return this.buildUserRO(savedUser);
7365
}
7466

7567
}
@@ -87,16 +79,20 @@ export class UserService {
8779
return await this.userRepository.delete({ email: email});
8880
}
8981

90-
async findById(id: number): Promise<UserEntity>{
82+
async findById(id: number): Promise<UserRO>{
9183
const user = await this.userRepository.findOneById(id);
92-
if (user) delete user.password;
93-
return user;
84+
85+
if (!user) {
86+
const errors = {User: ' not found'};
87+
throw new HttpException({errors}, 401);
88+
};
89+
90+
return this.buildUserRO(user);
9491
}
9592

96-
async findByEmail(email: string): Promise<UserEntity>{
93+
async findByEmail(email: string): Promise<UserRO>{
9794
const user = await this.userRepository.findOne({email: email});
98-
if (user) delete user.password;
99-
return user;
95+
return this.buildUserRO(user);
10096
}
10197

10298
public generateJWT(user) {
@@ -111,4 +107,16 @@ export class UserService {
111107
exp: exp.getTime() / 1000,
112108
}, SECRET);
113109
};
110+
111+
private buildUserRO(user: UserEntity) {
112+
const userRO = {
113+
username: user.username,
114+
email: user.email,
115+
bio: user.bio,
116+
token: this.generateJWT(user),
117+
image: user.image
118+
};
119+
120+
return {user: userRO};
121+
}
114122
}

0 commit comments

Comments
 (0)