Skip to content

Commit

Permalink
refactor: add more auth usecases
Browse files Browse the repository at this point in the history
  • Loading branch information
nunocaseiro committed Mar 13, 2023
1 parent 590a2fb commit 7f22dec
Show file tree
Hide file tree
Showing 26 changed files with 136 additions and 193 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { MailerService } from '@nestjs-modules/mailer';
import { Inject, Injectable, InternalServerErrorException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { MailerService } from '@nestjs-modules/mailer';
import { CreateResetTokenAuthServiceInterface } from '../interfaces/services/create-reset-token.auth.service.interface';
import { TYPES } from 'src/modules/auth/interfaces/types';
import { CreateResetTokenUseCaseInterface } from '../interfaces/applications/create-reset-token.use-case.interface';
import { TYPES } from '../interfaces/types';
import { ResetPasswordRepositoryInterface } from '../repository/reset-password.repository.interface';

@Injectable()
export default class CreateResetTokenAuthService implements CreateResetTokenAuthServiceInterface {
export class CreateResetTokenUseCase implements CreateResetTokenUseCaseInterface {
constructor(
private mailerService: MailerService,
private configService: ConfigService,
@Inject(TYPES.repository.ResetPasswordRepository)
private readonly resetPasswordRepository: ResetPasswordRepositoryInterface
) {}

async create(emailAddress: string) {
async execute(emailAddress: string) {
await this.resetPasswordRepository.startTransaction();
try {
const passwordModel = await this.resetPasswordRepository.findPassword(emailAddress);
Expand Down

This file was deleted.

16 changes: 16 additions & 0 deletions backend/src/modules/auth/applications/refresh-token.use-case.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Inject, Injectable } from '@nestjs/common';
import { RefreshTokenUseCaseInterface } from '../interfaces/applications/refresh-token.use-case.interface';
import { TYPES } from '../interfaces/types';
import { GetTokenAuthServiceInterface } from '../interfaces/services/get-token.auth.service.interface';

@Injectable()
export default class RefreshTokenUseCase implements RefreshTokenUseCaseInterface {
constructor(
@Inject(TYPES.services.GetTokenAuthService)
private readonly getTokenService: GetTokenAuthServiceInterface
) {}

async execute(userId: string) {
return this.getTokenService.getAccessToken(userId);
}
}
32 changes: 32 additions & 0 deletions backend/src/modules/auth/applications/reset-password.use-case.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { BadRequestException, Inject, Injectable, UnauthorizedException } from '@nestjs/common';
import { TYPES } from 'src/modules/users/interfaces/types';
import { ResetPasswordUseCaseInterface } from '../interfaces/applications/reset-password.use-case.interface';
import { UpdateUserServiceInterface } from 'src/modules/users/interfaces/services/update.user.service.interface';
import { UPDATE_FAILED } from 'src/libs/exceptions/messages';

@Injectable()
export default class ResetPasswordUseCase implements ResetPasswordUseCaseInterface {
constructor(
@Inject(TYPES.services.UpdateUserService)
private readonly updateUserService: UpdateUserServiceInterface
) {}

async execute(token: string, newPassword: string, newPasswordConf: string) {
const email = await this.updateUserService.checkEmailOfToken(token);

if (!email) {
throw new UnauthorizedException('Invalid token!');
}

const result = await this.updateUserService.setPassword(email, newPassword, newPasswordConf);

if (!result) {
throw new BadRequestException(UPDATE_FAILED);
}

return {
status: 'ok',
message: 'Password updated successfully!'
};
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Inject, Injectable } from '@nestjs/common';
import { TYPES } from 'src/modules/users/interfaces/types';
import { ValidateUserEmailUseCaseInterface } from '../interfaces/applications/validate-email.use-case.interface';
import { UserRepositoryInterface } from 'src/modules/users/repository/user.repository.interface';
import { GetUserServiceInterface } from 'src/modules/users/interfaces/services/get.user.service.interface';

@Injectable()
export default class ValidateUserEmailUseCase implements ValidateUserEmailUseCaseInterface {
constructor(
@Inject(TYPES.repository)
private readonly userRepository: UserRepositoryInterface
@Inject(TYPES.services.GetUserService)
private readonly getUserService: GetUserServiceInterface
) {}

async execute(email: string) {
const user = await this.userRepository.findOneByField({ email });
const user = await this.getUserService.getByEmail(email);

return !!user;
}
Expand Down
16 changes: 7 additions & 9 deletions backend/src/modules/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ import BoardsModule from '../boards/boards.module';
import TeamsModule from '../teams/teams.module';
import UsersModule from '../users/users.module';
import {
createResetTokenAuthApplication,
createResetTokenAuthService,
getTokenAuthApplication,
createResetTokenUseCase,
getTokenAuthService,
registerAuthService,
refreshTokenUseCase,
registerGuestUserUseCase,
registerUserUseCase,
resetPasswordRepository,
resetPasswordUseCase,
signInUseCase,
statisticsAuthUserUseCase,
validateUserAuthService,
Expand All @@ -44,16 +43,15 @@ import JwtRefreshTokenStrategy from './strategy/refresh.strategy';
],
providers: [
getTokenAuthService,
registerAuthService,
validateUserAuthService,
getTokenAuthApplication,
registerUserUseCase,
registerGuestUserUseCase,
validateUserEmailUseCase,
statisticsAuthUserUseCase,
signInUseCase,
createResetTokenAuthApplication,
createResetTokenAuthService,
refreshTokenUseCase,
createResetTokenUseCase,
resetPasswordUseCase,
UsersModule,
userRepository,
LocalStrategy,
Expand All @@ -62,6 +60,6 @@ import JwtRefreshTokenStrategy from './strategy/refresh.strategy';
resetPasswordRepository
],
controllers: [AuthController],
exports: [getTokenAuthService]
exports: [getTokenAuthService, resetPasswordRepository]
})
export default class AuthModule {}
40 changes: 17 additions & 23 deletions backend/src/modules/auth/auth.providers.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,25 @@
import { CreateResetTokenAuthApplication } from './applications/create-reset-token.auth.application';
import { GetTokenAuthApplication } from './applications/get-token.auth.application';
import { CreateResetTokenUseCase } from './applications/create-reset-token.use-case';
import RefreshTokenUseCase from './applications/refresh-token.use-case';
import RegisterGuestUserUseCase from './applications/register-guest-user.use-case';
import RegisterUserUseCase from './applications/register-user.use-case';
import ResetPasswordUseCase from './applications/reset-password.use-case';
import SignInUseCase from './applications/signIn.use-case';
import StatisticsAuthUserUseCase from './applications/statistics.auth.application';
import StatisticsAuthUserUseCase from './applications/statistics.auth.use-case';
import ValidateUserEmailUseCase from './applications/validate-user-email.use-case';
import { TYPES } from './interfaces/types';
import { ResetPasswordRepository } from './repository/reset-password.repository';
import CreateResetTokenAuthService from './services/create-reset-token.auth.service';
import GetTokenAuthService from './services/get-token.auth.service';
import RegisterAuthService from './services/register.auth.service';

export const getTokenAuthService = {
provide: TYPES.services.GetTokenAuthService,
useClass: GetTokenAuthService
};

export const registerAuthService = {
provide: TYPES.services.RegisterAuthService,
useClass: RegisterAuthService
};

export const validateUserAuthService = {
provide: TYPES.services.ValidateAuthService,
useClass: ValidateUserEmailUseCase
};

export const createResetTokenAuthService = {
provide: TYPES.services.CreateResetTokenAuthService,
useClass: CreateResetTokenAuthService
};

export const getTokenAuthApplication = {
provide: TYPES.applications.GetTokenAuthApplication,
useClass: GetTokenAuthApplication
};

export const registerUserUseCase = {
provide: TYPES.applications.RegisterUserUseCase,
useClass: RegisterUserUseCase
Expand All @@ -46,6 +30,11 @@ export const signInUseCase = {
useClass: SignInUseCase
};

export const refreshTokenUseCase = {
provide: TYPES.applications.RefreshTokenUseCase,
useClass: RefreshTokenUseCase
};

export const registerGuestUserUseCase = {
provide: TYPES.applications.RegisterGuestUserUseCase,
useClass: RegisterGuestUserUseCase
Expand All @@ -56,14 +45,19 @@ export const statisticsAuthUserUseCase = {
useClass: StatisticsAuthUserUseCase
};

export const resetPasswordUseCase = {
provide: TYPES.applications.ResetPasswordUseCase,
useClass: ResetPasswordUseCase
};

export const validateUserEmailUseCase = {
provide: TYPES.applications.ValidateUserEmailUseCase,
useClass: ValidateUserEmailUseCase
};

export const createResetTokenAuthApplication = {
provide: TYPES.applications.CreateResetTokenAuthApplication,
useClass: CreateResetTokenAuthApplication
export const createResetTokenUseCase = {
provide: TYPES.applications.CreateResetTokenUseCase,
useClass: CreateResetTokenUseCase
};

export const resetPasswordRepository = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import * as User from 'src/modules/users/interfaces/types';
import { createMock } from '@golevelup/ts-jest';
import { RegisterAuthApplication } from '../applications/register.auth.application';
import { GetTeamApplication } from 'src/modules/teams/applications/get.team.application';
import { CreateResetTokenAuthApplication } from '../applications/create-reset-token.auth.application';
import { CreateResetTokenAuthApplication } from '../applications/create-reset-token.use-case';
import { UpdateUserApplication } from 'src/modules/users/use-cases/update.user.application';
import { GetBoardApplication } from 'src/modules/boards/applications/get.board.application';
import { GetUserApplication } from 'src/modules/users/use-cases/get.user.application';
Expand Down
46 changes: 14 additions & 32 deletions backend/src/modules/auth/controller/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
BadRequestException,
Body,
Controller,
Get,
Expand All @@ -10,7 +9,6 @@ import {
Patch,
Post,
Req,
UnauthorizedException,
UseGuards
} from '@nestjs/common';
import {
Expand All @@ -27,7 +25,6 @@ import {
ApiUnauthorizedResponse
} from '@nestjs/swagger';
import { EmailParam } from 'src/libs/dto/param/email.param';
import { UPDATE_FAILED } from 'src/libs/exceptions/messages';
import JwtAuthenticationGuard from 'src/libs/guards/jwtAuth.guard';
import JwtRefreshGuard from 'src/libs/guards/jwtRefreshAuth.guard';
import LocalAuthGuard from 'src/libs/guards/localAuth.guard';
Expand All @@ -39,18 +36,18 @@ import { UnauthorizedResponse } from 'src/libs/swagger/errors/unauthorized.swagg
import CreateUserDto from 'src/modules/users/dto/create.user.dto';
import { ResetPasswordDto } from 'src/modules/users/dto/reset-password.dto';
import UserDto from 'src/modules/users/dto/user.dto';
import { UpdateUserApplicationInterface } from 'src/modules/users/interfaces/applications/update.user.service.interface';
import { LoginDto } from '../dto/login.dto';
import { TYPES } from '../interfaces/types';
import { LoginResponse } from '../swagger/login.swagger';
import CreateGuestUserDto from 'src/modules/users/dto/create.guest.user.dto';
import { CreateResetTokenAuthApplicationInterface } from '../interfaces/applications/create-reset-token.auth.application.interface';
import { GetTokenAuthApplicationInterface } from '../interfaces/applications/get-token.auth.application.interface';
import { RegisterUserUseCaseInterface } from '../interfaces/applications/register-user.use-case.interface';
import { RegisterGuestUserUseCaseInterface } from '../interfaces/applications/register-guest-user.use-case.interface';
import { StatisticsAuthUserUseCaseInterface } from '../interfaces/applications/statistics.auth.use-case.interface';
import { ValidateUserEmailUseCaseInterface } from '../interfaces/applications/validate-email.use-case.interface';
import { SignInUseCaseInterface } from '../interfaces/applications/signIn.use-case.interface';
import { RefreshTokenUseCaseInterface } from '../interfaces/applications/refresh-token.use-case.interface';
import { ResetPasswordUseCaseInterface } from '../interfaces/applications/reset-password.use-case.interface';
import { CreateResetTokenUseCaseInterface } from '../interfaces/applications/create-reset-token.use-case.interface';

@ApiTags('Authentication')
@Controller('auth')
Expand All @@ -62,16 +59,16 @@ export default class AuthController {
private registerGuestUserUseCase: RegisterGuestUserUseCaseInterface,
@Inject(TYPES.applications.ValidateUserEmailUseCase)
private validateUserEmailUseCase: ValidateUserEmailUseCaseInterface,
@Inject(TYPES.applications.GetTokenAuthApplication)
private getTokenAuthApp: GetTokenAuthApplicationInterface,
@Inject(TYPES.applications.RefreshTokenUseCase)
private refreshTokenUseCase: RefreshTokenUseCaseInterface,
@Inject(TYPES.applications.StatisticsAuthUserUseCase)
private statisticsUseCase: StatisticsAuthUserUseCaseInterface,
@Inject(TYPES.applications.CreateResetTokenAuthApplication)
private createResetTokenAuthApp: CreateResetTokenAuthApplicationInterface,
@Inject(TYPES.applications.ResetPasswordUseCase)
private resetPasswordUseCase: ResetPasswordUseCaseInterface,
@Inject(TYPES.applications.CreateResetTokenUseCase)
private createResetTokenUseCase: CreateResetTokenUseCaseInterface,
@Inject(TYPES.applications.SignInUseCase)
private signInUseCase: SignInUseCaseInterface,
@Inject(TYPES.services.UpdateUserService)
private updateUserService: UpdateUserApplicationInterface
private signInUseCase: SignInUseCaseInterface
) {}

@ApiOperation({ summary: 'Create new user' })
Expand Down Expand Up @@ -151,7 +148,7 @@ export default class AuthController {
@UseGuards(JwtRefreshGuard)
@Get('refresh')
refresh(@Req() request: RequestWithUser) {
return this.getTokenAuthApp.getAccessToken(request.user._id);
return this.refreshTokenUseCase.execute(request.user._id);
}

@ApiParam({
Expand Down Expand Up @@ -193,7 +190,7 @@ export default class AuthController {
})
@Patch('password/reset')
forgot(@Body() { email }: EmailParam) {
return this.createResetTokenAuthApp.create(email);
return this.createResetTokenUseCase.execute(email);
}

@ApiOperation({
Expand Down Expand Up @@ -229,23 +226,8 @@ export default class AuthController {
})
@Patch('password')
@HttpCode(HttpStatus.OK)
async setNewPassword(@Body() { token, newPassword, newPasswordConf }: ResetPasswordDto) {
const email = await this.updateUserService.checkEmail(token);

if (!email) {
throw new UnauthorizedException('Invalid token!');
}

const result = await this.updateUserService.setPassword(email, newPassword, newPasswordConf);

if (!result) {
throw new BadRequestException(UPDATE_FAILED);
}

return {
status: 'ok',
message: 'Password updated successfully!'
};
setNewPassword(@Body() { token, newPassword, newPasswordConf }: ResetPasswordDto) {
return this.resetPasswordUseCase.execute(token, newPassword, newPasswordConf);
}

@ApiOperation({
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface CreateResetTokenUseCaseInterface {
execute(emailAddress: string): Promise<{
message: string;
}>;
}
Loading

0 comments on commit 7f22dec

Please sign in to comment.