refactor: move error messages from controller to service

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2022-01-17 08:54:08 +01:00
parent f672c5179f
commit 4500caf882
2 changed files with 11 additions and 21 deletions

View file

@ -75,25 +75,15 @@ export class AuthController {
@RequestUser() user: User,
@Body() changePasswordDto: UpdatePasswordDto,
): Promise<void> {
try {
await this.identityService.checkLocalPassword(
user,
changePasswordDto.currentPassword,
);
await this.identityService.updateLocalPassword(
user,
changePasswordDto.newPassword,
);
return;
} catch (e) {
if (e instanceof InvalidCredentialsError) {
throw new UnauthorizedException('Password is not correct');
}
if (e instanceof NoLocalIdentityError) {
throw new BadRequestException('User has no local identity.');
}
throw e;
}
await this.identityService.checkLocalPassword(
user,
changePasswordDto.currentPassword,
);
await this.identityService.updateLocalPassword(
user,
changePasswordDto.newPassword,
);
return;
}
@UseGuards(LoginEnabledGuard, LocalAuthGuard)

View file

@ -85,14 +85,14 @@ export class IdentityService {
`The user with the username ${user.username} does not have a internal identity.`,
'checkLocalPassword',
);
throw new NoLocalIdentityError();
throw new NoLocalIdentityError('This user has no internal identity.');
}
if (!(await checkPassword(password, internalIdentity.passwordHash ?? ''))) {
this.logger.debug(
`Password check for ${user.username} did not succeed.`,
'checkLocalPassword',
);
throw new InvalidCredentialsError();
throw new InvalidCredentialsError('Password is not correct');
}
}
}