Skip to content

Commit

Permalink
refactor: uuid to mongoid
Browse files Browse the repository at this point in the history
  • Loading branch information
nunocaseiro committed Nov 17, 2021
1 parent b557813 commit b2216c6
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 23 deletions.
7 changes: 5 additions & 2 deletions backend/src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { LocalStrategy } from './strategy/local.strategy';
import { JwtStrategy } from './strategy/jwt.strategy';
import { JwtRefreshTokenStrategy } from './strategy/refresh.strategy';
import {
describe,
JWT_ACCESS_TOKEN_SECRET,
JWT_ACCESS_TOKEN_EXPIRATION_TIME,
} from '../constants/jwt';
Expand All @@ -22,9 +23,11 @@ import {
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) => ({
secret: configService.get(JWT_ACCESS_TOKEN_SECRET),
secret: configService.get(describe(JWT_ACCESS_TOKEN_SECRET)),
signOptions: {
expiresIn: `${configService.get(JWT_ACCESS_TOKEN_EXPIRATION_TIME)}s`,
expiresIn: `${configService.get(
describe(JWT_ACCESS_TOKEN_EXPIRATION_TIME),
)}s`,
},
}),
}),
Expand Down
19 changes: 13 additions & 6 deletions backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
JWT_ACCESS_TOKEN_SECRET,
JWT_REFRESH_TOKEN_EXPIRATION_TIME,
JWT_REFRESH_TOKEN_SECRET,
describe,
} from '../constants/jwt';
import { INVALID_CREDENTIALS, EMAIL_EXISTS } from '../constants/httpExceptions';

Expand Down Expand Up @@ -63,11 +64,15 @@ export class AuthService {
public getJwtAccessToken(userId: string) {
const payload: TokenPayload = { userId };
const token = this.jwtService.sign(payload, {
secret: this.configService.get(JWT_ACCESS_TOKEN_SECRET),
expiresIn: `${this.configService.get(JWT_ACCESS_TOKEN_EXPIRATION_TIME)}s`,
secret: this.configService.get(describe(JWT_ACCESS_TOKEN_SECRET)),
expiresIn: `${this.configService.get(
describe(JWT_ACCESS_TOKEN_EXPIRATION_TIME),
)}s`,
});
return {
expiresIn: this.configService.get(JWT_ACCESS_TOKEN_EXPIRATION_TIME),
expiresIn: this.configService.get(
describe(JWT_ACCESS_TOKEN_EXPIRATION_TIME),
),
token,
};
}
Expand All @@ -76,13 +81,15 @@ export class AuthService {
const payload: TokenPayload = { userId };

const token = this.jwtService.sign(payload, {
secret: this.configService.get(JWT_REFRESH_TOKEN_SECRET),
secret: this.configService.get(describe(JWT_REFRESH_TOKEN_SECRET)),
expiresIn: `${this.configService.get(
JWT_REFRESH_TOKEN_EXPIRATION_TIME,
describe(JWT_REFRESH_TOKEN_EXPIRATION_TIME),
)}d`,
});
return {
expiresIn: this.configService.get(JWT_REFRESH_TOKEN_EXPIRATION_TIME),
expiresIn: this.configService.get(
describe(JWT_REFRESH_TOKEN_EXPIRATION_TIME),
),
token,
};
}
Expand Down
4 changes: 2 additions & 2 deletions backend/src/boards/boards.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class BoardsService {
async getBoardFromRepo(boardId: string) {
const board = await this.boardsRepository.findOne({
where: {
_id: new ObjectId(boardId),
_id: boardId,
},
});
if (board) return board;
Expand All @@ -47,7 +47,7 @@ export class BoardsService {
if (!board.locked) return board;

const isPasswordMatched = await compare(password, board.password);
if (board.locked && password && isPasswordMatched) return board;
if (password && isPasswordMatched) return board;

return { locked: true };
}
Expand Down
6 changes: 2 additions & 4 deletions backend/src/boards/dto/card.dto.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { IsEmail, IsNotEmpty, IsString } from 'class-validator';
import { UserDto } from 'src/users/dto/user.dto';
import { v4 as uuidv4 } from 'uuid';
import { ObjectID } from 'mongodb';

class CardDto {
@IsString()
_id: string = uuidv4();
_id: ObjectID = ObjectID();

@IsNotEmpty()
title: string;

@IsEmail()
@IsNotEmpty()
createdBy: UserDto;
}
Expand Down
5 changes: 2 additions & 3 deletions backend/src/boards/dto/column.dto.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { IsString, IsNotEmpty, ValidateNested } from 'class-validator';
import { v4 as uuidv4 } from 'uuid';
import { Transform, TransformFnParams, Type } from 'class-transformer';
import CardDto from './card.dto';
import { ObjectID } from 'mongodb';

export class ColumnDto {
@IsString()
_id: string = uuidv4();
_id: ObjectID = ObjectID();

@IsNotEmpty()
@IsString()
Expand Down
27 changes: 21 additions & 6 deletions backend/src/constants/jwt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
export const JWT_REFRESH_TOKEN_SECRET = 'JWT_REFRESH_TOKEN_SECRET';
export const JWT_REFRESH_TOKEN_EXPIRATION_TIME =
'JWT_REFRESH_TOKEN_EXPIRATION_TIME';
export const JWT_REFRESH_TOKEN_SECRET = Symbol();
export const JWT_REFRESH_TOKEN_EXPIRATION_TIME = Symbol(
'JWT_REFRESH_TOKEN_EXPIRATION_TIME',
);

export const JWT_ACCESS_TOKEN_SECRET = 'JWT_ACCESS_TOKEN_SECRET';
export const JWT_ACCESS_TOKEN_EXPIRATION_TIME =
'JWT_ACCESS_TOKEN_EXPIRATION_TIME';
export const JWT_ACCESS_TOKEN_SECRET = Symbol('JWT_ACCESS_TOKEN_SECRET');
export const JWT_ACCESS_TOKEN_EXPIRATION_TIME = Symbol(
'JWT_ACCESS_TOKEN_EXPIRATION_TIME',
);

export function describe(key: symbol): string {
switch (key) {
case JWT_REFRESH_TOKEN_SECRET:
return 'JWT_REFRESH_TOKEN_SECRET';
case JWT_REFRESH_TOKEN_EXPIRATION_TIME:
return 'JWT_REFRESH_TOKEN_EXPIRATION_TIME';
case JWT_ACCESS_TOKEN_SECRET:
return 'JWT_ACCESS_TOKEN_SECRET';
case JWT_ACCESS_TOKEN_EXPIRATION_TIME:
return 'JWT_ACCESS_TOKEN_EXPIRATION_TIME';
}
}

0 comments on commit b2216c6

Please sign in to comment.