HM-109. Добавлена ручка для смены пароля

This commit is contained in:
vigdorov
2020-08-08 16:14:05 +03:00
parent 96d3232f9f
commit d4f6032232
5 changed files with 104 additions and 32 deletions

View File

@ -198,7 +198,7 @@ export class UserService {
} catch (e) {
return false;
}
const token = jwt.decode(access_token) as Token;
const searchUser = await this.findUser(token.login);
return searchUser && this.checkToken(token, agent);
@ -216,4 +216,19 @@ export class UserService {
login,
});
}
}
async changePassword(access_token: string, old_password: string, new_password: string): Promise<string> {
const {login} = jwt.decode(access_token) as Token;
const user = await this.userModel().findOne({login});
if (user && await this.checkPassword(old_password, user.password)) {
const salt = user.salt;
const password = await bcrypt.hash(new_password, salt);
await user.updateOne({
password,
});
return 'ok';
}
throw new BadRequestException('Unauthorized request');
}
}