Skip to content

Commit

Permalink
use exclude function
Browse files Browse the repository at this point in the history
  • Loading branch information
4H1R committed Aug 8, 2022
1 parent 7c77489 commit 1301784
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
6 changes: 0 additions & 6 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
version: '3.8'
services:
mailhog:
image: mailhog/mailhog:latest
restart: always
ports:
- 1025:1025
- 8025:8025
database:
image: postgres:13
ports:
Expand Down
5 changes: 2 additions & 3 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ export class AuthController {
@ApiBearerAuth()
@Get('me')
@HttpCode(HttpStatus.OK)
async me(@Req() req) {
me(@Req() req) {
const user = req.user as User;
delete user.password;
return user;
return this.authService.me(user);
}
}
16 changes: 8 additions & 8 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ValidationErrorException } from 'src/common/exception';
import { LoginDto, RefreshTokenDto, RegisterDto } from './dto';
import { User } from '@prisma/client';
import * as bcrypt from 'bcrypt';
import { exclude } from 'src/utils';

@Injectable()
export class AuthService {
Expand All @@ -20,7 +21,7 @@ export class AuthService {
email: 'This email has already been taken.',
});

const password = await this.hash(registerDto.password);
const password = await bcrypt.hash(registerDto.password, 10);
const createdUser = await this.prisma.user.create({
data: {
...registerDto,
Expand All @@ -46,7 +47,7 @@ export class AuthService {

public async refresh(refreshTokenDto: RefreshTokenDto) {
try {
const token: any = this.jwt.verify(refreshTokenDto.refreshToken);
const token = this.jwt.verify(refreshTokenDto.refreshToken);
// we specifically sign refreshToken with type 'refresh'
if (token.type !== 'refresh') throw new UnauthorizedException();
const user = await this.prisma.user.findUnique({
Expand All @@ -61,8 +62,11 @@ export class AuthService {
}
}

public me(user: User) {
return exclude(user, 'password');
}

private generateTokens(user: User) {
delete user.password;
const payload = { email: user.email, sub: user.id };

return {
Expand All @@ -71,14 +75,10 @@ export class AuthService {
{ ...payload, type: 'refresh' },
{ expiresIn: '1w' },
),
user,
user: this.me(user),
};
}

private async hash(password: string) {
return await bcrypt.hash(password, 10);
}

private throwInvalidCredentials() {
throw new ValidationErrorException({
email: "These credentials doesn't match our records.",
Expand Down
8 changes: 8 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ export const transformValidationErrors = (e: ValidationError[]) => {
}, {} as Record<string, string[]>);
throw new ValidationErrorException(errors);
};

export const exclude = <T extends Record<string, unknown>, Key extends keyof T>(
user: T,
...keys: Key[]
): Omit<T, Key> => {
for (const key of keys) delete user[key];
return user;
};

0 comments on commit 1301784

Please sign in to comment.