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, @RequestUser() user: User,
@Body() changePasswordDto: UpdatePasswordDto, @Body() changePasswordDto: UpdatePasswordDto,
): Promise<void> { ): Promise<void> {
try { await this.identityService.checkLocalPassword(
await this.identityService.checkLocalPassword( user,
user, changePasswordDto.currentPassword,
changePasswordDto.currentPassword, );
); await this.identityService.updateLocalPassword(
await this.identityService.updateLocalPassword( user,
user, changePasswordDto.newPassword,
changePasswordDto.newPassword, );
); return;
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;
}
} }
@UseGuards(LoginEnabledGuard, LocalAuthGuard) @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.`, `The user with the username ${user.username} does not have a internal identity.`,
'checkLocalPassword', 'checkLocalPassword',
); );
throw new NoLocalIdentityError(); throw new NoLocalIdentityError('This user has no internal identity.');
} }
if (!(await checkPassword(password, internalIdentity.passwordHash ?? ''))) { if (!(await checkPassword(password, internalIdentity.passwordHash ?? ''))) {
this.logger.debug( this.logger.debug(
`Password check for ${user.username} did not succeed.`, `Password check for ${user.username} did not succeed.`,
'checkLocalPassword', 'checkLocalPassword',
); );
throw new InvalidCredentialsError(); throw new InvalidCredentialsError('Password is not correct');
} }
} }
} }